str_starts_with
PHP function
Edit on GitHub ✎
(PHP 8)
Checks if a string starts with a given substring
Description
str_starts_with(string $haystack, string $needle): bool
Performs a case-sensitive check indicating if haystack begins with needle.
Parameters
haystackThe string to search in.
needleThe substring to search for in the
haystack.
Return Values
Returns true if haystack begins with needle, false otherwise.
Examples
Using the empty string ''
php
<?php
if (str_starts_with('abc', '')) {
echo "All strings start with the empty string";
}
?>The above example will output:
output
All strings start with the empty stringShowing case-sensitivity
php
<?php
$string = 'The lazy fox jumped over the fence';
if (str_starts_with($string, 'The')) {
echo "The string starts with 'The'\n";
}
if (str_starts_with($string, 'the')) {
echo 'The string starts with "the"';
} else {
echo '"the" was not found because the case does not match';
}
?>The above example will output:
output
The string starts with 'The'
"the" was not found because the case does not match