Home  >  Article  >  Backend Development  >  Let's talk about the str_starts_with() function in PHP8

Let's talk about the str_starts_with() function in PHP8

藏色散人
藏色散人Original
2021-08-30 10:44:343564browse

In the previous article "Why is the === operator in PHP faster than ==? 》Introduced to you why the === operator in PHP is faster than ==. Interested friends can read and learn about it~

This article will take you to talk about the === operator in PHP8 str_starts_with()Function.

str_starts_with() is a predefined function in PHP 8 that performs a case-sensitive search on a given string. str_starts_with() usually checks whether the string starts with a substring. str_starts_with() will return TRUE if the string starts with a substring, otherwise it will return FALSE.

str_starts_with() syntax is:

str_starts_with($string, $substring)

$string: This parameter refers to the string that needs to check the starting string.

$substring: This parameter refers to the string that needs to be checked.

Return value: If the string starts with a substring, str_starts_with() will return TRUE otherwise it will return FALSE.

str_starts_with() Main features:

  • str_starts_with() is essentially case-sensitive.

  • str_starts_with() always returns a Boolean value.

  • str_starts_with() can be used to check the beginning of characters and strings.

  • Versions smaller than PHP8 do not support str_starts_with().

The following uses 2 examples to introduce the use of this function:

Example 1:

<?php

	$name = &#39;Saurabh Singh&#39;;
	$beginsWith = &#39;S&#39;;

	$result = str_starts_with($name, $beginsWith) ? &#39;is&#39; : &#39;is not&#39;;

	echo "字符串 \"$name\" $result 以 $beginsWith 开头";

?>

Output :

字符串 "Saurabh Singh" 以 S 开头

In the above example we created three variables: $name to store the name of the string type, $begininswith to store the substring that needs to be checked with $name, $result to store the calculation based on str_starts_with() the result of the expression. str_starts_with() will return TRUE if the string $name begins with the substring $begininswith, otherwise it will return FALSE and assign the value of $result accordingly.

Example 2:

<?php

	$sentance = &#39;The Big Brown Fox&#39;;
	$beginsWith = &#39;The&#39;;

	if(str_starts_with($sentance , $beginsWith) )
	{
	echo "字符串 \"$sentance\" 以 \"$beginsWith\" 开头";
	}
	else
	{
	echo "字符串 \"$sentance\" 不以 \"$beginsWith\" 开头";
	}	

?>

Output:

字符串 "The Big Brown Fox" 以 "The" 开头

In example 1 , we search using the beginning characters of the sentence. In this example, we are taking the complete word at the beginning of a sentence, which will also return TRUE in the if condition, and then the conditional part will be executed accordingly.

Finally, I would like to recommend the latest and most comprehensive "PHP Video Tutorial"~ Come and learn!

The above is the detailed content of Let's talk about the str_starts_with() function in PHP8. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn