php8.5
Home/ Manual/ strings / functions/ str_starts_with

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

haystack

The string to search in.

needle

The 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 string

Showing 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

Notes Bin-safe

See Also

Source: reference/strings/functions/str-starts-with.xml · from the official PHP manual (php/doc-en)