The str_starts_with Function
The str_starts_with function checks if a
string starts with a given substring and returns
true on success, and false on failure.
The first parameter of the function is the string
to search in. The second parameter specifies the
desired character or substring.
Syntax
str_starts_with(string $haystack, string $needle): bool
Example
Let's check if the string starts with a given character:
<?php
$str = 'abcde';
$res = str_starts_with($str, 'a');
var_dump($res);
?>
The code execution result:
true
Example
Let's check if the string starts with a given substring:
<?php
$str = 'abcde';
$res = str_starts_with($str, 'ab');
var_dump($res);
?>
The code execution result:
true
Example
Let's check if the string starts with a given character:
<?php
$str = 'abcde';
$res = str_starts_with($str, 'x');
var_dump($res);
?>
The code execution result:
false
See Also
-
the
str_containsfunction,
which checks for the occurrence of a character in a string -
the
str_starts_withfunction,
which checks the start of a string -
the
strposfunction,
which returns the position of the substring occurrence