str_starts_with()
str_starts_with() — Checks if a string starts with a given substring.
Built-in function of PHP 8.5. The manual lists it as available in PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Performs a case-sensitive check indicating if haystack begins with needle.
Signature
str_starts_with(string $haystack, string $needle): bool
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $haystack | string | The string to search in. |
| $needle | string | The substring to search for in the haystack. |
Return value
Returns true if haystack begins with needle, false otherwise.
Example
A runnable snippet:
if (str_starts_with('abc', '')) { echo "All strings start with the empty string"; }
Output:
All strings start with the empty string
See also
str_contains(), str_ends_with(), stripos(), strrpos(), strripos(), strstr(), strpbrk(), substr(), preg_match()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page str_starts_with() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.