<span style="font-size: 14px;"> Comparative size of integer and floating point data in PHP</span>
, It is more convenient, but it is more troublesome to compare strings. How to compare string data types, PHP
provides the built-in function strcmp()
for easy use.
Syntax:
strcmp(string $str1 , string $str2 )
$str1: The first string.
$str2: The second string.
Equal case
<?php $a=strcmp("Hello world!","Hello world!"); echo $a."<br>"; //0 ?>
Greater than case
<?php $b=strcmp("Hello world!","Hello"); echo $b."<br>"; // 7 ?>
If it is less than
<?php $c=strcmp("Hello world!","Hello world! Hello!"); echo $c."<br>"; // -7 ?>
The comparison of the strcmp() function is based on the ASCII code size.
Recommendation: 《2021 PHP interview questions summary (collection)》《php video tutorial》
The above is the detailed content of How to use strcmp() function to compare strings in PHP. For more information, please follow other related articles on the PHP Chinese website!