Home  >  Article  >  Backend Development  >  How does PHP determine whether a string, etc. is equal to the specified value?

How does PHP determine whether a string, etc. is equal to the specified value?

青灯夜游
青灯夜游Original
2022-09-27 20:04:513174browse

Judgment method: 1. Use the strcmp() function to judge, the syntax is “strcmp(string, specified value)”, if they are equal, return 0; 2. Use the strcasecmp() function to judge, the syntax is “strcasecmp(character) String, specified value)", if equal, return 0; 3. Use the "==" or "===" operator to judge, the syntax is "string == specified value" or "string === specified value".

How does PHP determine whether a string, etc. is equal to the specified value?

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php Determine if the string is not equal to the specified value

Method 1: Use the strcmp() function to determine

The strcmp() function can compare two Strings are compared in a binary safe manner and are case-sensitive. The syntax format is as follows:

strcmp($str1, $str2)

where $str1 and $str2 are the two strings to be compared.

The strcmp() function will return different values ​​according to the comparison result. If $str1 is less than $str2, the return value ; if $str1 is greater than $str2, the return value<code>> 0;If $str1 and $str2 are equal, return 0.

The sample code is as follows:

<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;hello&#39;;
if(strcmp($str, "Hello") != 0){
    echo &#39;字符串不等于指定值&#39;;
} else {
    echo &#39;字符串等于指定值&#39;;
}
?>

How does PHP determine whether a string, etc. is equal to the specified value?

Note that for the strcmp() function, the two strings to be compared must match exactly to be considered equal. . If you want to compare two strings in a case-insensitive manner, you can use the strcasecmp() function.

Method 2: Use the strcasecmp() function to determine

The strcasecmp() function in PHP has similar functions to the strcmp() function and can both compare two strings. , the difference is that the strcasecmp() function is not case-sensitive when comparing strings, and its syntax format is as follows:

strcasecmp($str1, $str2)

Among them, $str1 and $str2 are the two strings to be compared.

According to the comparison result, if $str1 is less than $str2, the return value is 0; if $str1 is equal to $str2, then 0 is returned.

The sample code is as follows:

<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;hello&#39;;
if(strcasecmp($str, "Hello") != 0){
    echo &#39;字符串不等于指定值&#39;;
} else {
    echo &#39;字符串等于指定值&#39;;
}
?>

How does PHP determine whether a string, etc. is equal to the specified value?

Method 3: Use the "==" or "===" operator to determine

"==" is the equal operator. As long as the values ​​of $a and $b are equal, it returns TRUE, otherwise it returns FALSE.

"===" is the equality operator, which returns TRUE if $a and $b are not only equal in value, but also have equal types of their values, otherwise it returns FALSE.

The sample code is as follows:

<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;123&#39;;
if($str==123){
    echo &#39;字符串等于指定值<br>&#39;;
} else {
    echo &#39;字符串不等于指定值<br>&#39;;
}
if($str===123){
    echo &#39;字符串等于指定值<br>&#39;;
} else {
    echo &#39;字符串不等于指定值<br>&#39;;
}
?>

How does PHP determine whether a string, etc. is equal to the specified value?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How does PHP determine whether a string, etc. is equal to the specified value?. 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