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".
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 < 0
; if $str1 is greater than $str2, the return value> 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 = 'hello'; if(strcmp($str, "Hello") != 0){ echo '字符串不等于指定值'; } else { echo '字符串等于指定值'; } ?>
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 greater 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 = 'hello'; if(strcasecmp($str, "Hello") != 0){ echo '字符串不等于指定值'; } else { echo '字符串等于指定值'; } ?>
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 = '123'; if($str==123){ echo '字符串等于指定值<br>'; } else { echo '字符串不等于指定值<br>'; } if($str===123){ echo '字符串等于指定值<br>'; } else { echo '字符串不等于指定值<br>'; } ?>
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!