Current Location: Home> Function Categories> str_starts_with

str_starts_with

Check if the string begins with the given substring
Name:str_starts_with
Category:String
Programming Language:php
One-line Description:Determines whether a string starts with the specified prefix

Function name: str_starts_with()

Function function: determines whether a string starts with the specified prefix.

Applicable version: PHP 8.0.0 and above.

Syntax: bool str_starts_with ( string $haystack , string $needle )

parameter:

  • $haystack: The string to be checked.
  • $needle: The prefix to look for.

Return value:

  • Returns true if the $haystack string starts with the $needle prefix.
  • If the $haystack string does not start with the $needle prefix, false is returned.

Example:

 $haystack = "Hello, world!"; $needle = "Hello"; if (str_starts_with($haystack, $needle)) { echo "字符串以指定前缀开始"; } else { echo "字符串不以指定前缀开始"; }

Output:

字符串以指定前缀开始

Notes:

  • The function is case sensitive, i.e. the prefix must exactly match the beginning part of the string.
  • If $needle is an empty string, the function will always return true.
  • In versions prior to PHP 8.0.0, the substr() function can be used to implement similar functions. For example: substr($haystack, 0, strlen($needle)) === $needle .
Similar Functions
Popular Articles