|
| 1 | +--- |
| 2 | +Title: 'str_starts_with()' |
| 3 | +Description: 'Performs a case-sensitive search to check if a string starts with a given substring.' |
| 4 | +Subjects: |
| 5 | + - 'Web Development' |
| 6 | + - 'Web Design' |
| 7 | + - 'Computer Science' |
| 8 | +Tags: |
| 9 | + - 'Strings' |
| 10 | + - 'Functions' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-php' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +The **`str_starts_with()`** function is used to perform a case-sensitive search to check whether a string starts with a given substring. |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +```pseudo |
| 21 | +$stringstartswith = str_starts_with($str, $substring); |
| 22 | +``` |
| 23 | + |
| 24 | +The `$str` represents the input string to check. |
| 25 | + |
| 26 | +The `$substr` represents the substring to search for in the input string. |
| 27 | + |
| 28 | +The `str_starts_with()` function returns true if the `$str` starts with the `$substr` or false otherwise. |
| 29 | + |
| 30 | +## Example |
| 31 | + |
| 32 | +The example below demonstrates `str_starts_with()` operating on the same string in three ways (single character, multiple characters, and a case-sensitive example). |
| 33 | + |
| 34 | +```php |
| 35 | +<?php |
| 36 | +$str = 'Peanut Butter Jelly'; |
| 37 | +$substr = 'P'; |
| 38 | +$stringstartswith = str_starts_with($str, $substr) ? 'begins' : 'does not begin'; |
| 39 | + |
| 40 | +echo ("the string $str $stringstartswith with $substr.\n"); |
| 41 | + |
| 42 | +$str = 'Peanut Butter Jelly'; |
| 43 | +$substr = 'Peanut'; |
| 44 | +$stringstartswith = str_starts_with($str, $substr) ? 'begins' : 'does not begin'; |
| 45 | + |
| 46 | +echo "the string $str $stringstartswith with $substr.\n"; |
| 47 | + |
| 48 | +$str = 'Peanut Butter Jelly'; |
| 49 | +$substr = 'p'; |
| 50 | +$stringstartswith = str_starts_with($str, $substr) ? 'begins' : 'does not begin'; |
| 51 | + |
| 52 | +echo "the string $str $stringstartswith with $substr.\n"; |
| 53 | +?> |
| 54 | +``` |
| 55 | + |
| 56 | +This will result in the following output: |
| 57 | + |
| 58 | +```shell |
| 59 | +the string Peanut Butter Jelly begins with P. |
| 60 | +the string Peanut Butter Jelly begins with Peanut. |
| 61 | +the string Peanut Butter Jelly does not begin with p. |
| 62 | +``` |
0 commit comments