search
HomeBackend DevelopmentPHP8Let's talk about the str_starts_with() function in PHP8

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

Aug 30, 2021 am 10:44 AM
php8str_starts_with()

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools