str_starts_with()
Checks if the string starts with the specified substring. Case sensitive.
This is a polyfill for the new php function str_starts_with() introduced in PHP version 8.0.
See also similar functions:
1 time — 0.000001 sec (speed of light) | 50000 times — 0.001 sec (speed of light)
No Hooks.
Returns
true|false. True if the string ($haystack) starts with the substring ($needle), false otherwise.
Usage
str_starts_with( $haystack, $needle );
- $haystack(string) (required)
- Hay - the string we are checking.
- $needle(string) (required)
- Needle - the substring that should be at the beginning of another string.
Examples
#1 Demo
str_starts_with( 'ABC', 'A' ); // true str_starts_with( 'ABC', 'C' ); // false // case sensitive str_starts_with( 'ABC', 'a' ); // false // all lines begin with an empty string str_starts_with( 'abc', '' ); // true
Changelog
| Since 5.9.0 | Introduced. |
str_starts_with() str starts with code WP 6.9
function str_starts_with( $haystack, $needle ) {
if ( '' === $needle ) {
return true;
}
return 0 === strpos( $haystack, $needle );
}