在PHP中,字符串函数 strcmp() 用来比较字符串,此函数区分大小写。
函数语法:strcmp ( string $str1 , string $str2 ) : int
函数参数说明:参数描述
str1必需。规定要比较的第一个字符串。
str2必需。规定要比较的第二个字符串。
strcmp() 用来比较两个字符串,此函数是二进制安全的,和 strcasecmp() 函数相反,此函数区分比较字符串的大小写。
它的返回值有下面三种情况:< 0:字符串 str1 小于 str2
> 0:字符串 str1 大于 str2
= 0:两者相等,返回 0
举例1,两个字符串相等:<?php
// 二进制安全比较字符串(区分大小写)
$res = strcmp('hello', 'hello');
// 输出0
echo $res;
以上代码输出如下:0
举例2,字符串str1 小于 str2:<?php
// 二进制安全比较字符串(区分大小写)
$res = strcmp('Hello', 'hello');
// 输出-1
echo $res;
以上代码输出如下:-1
举例3,字符串str1 大于 str2:<?php
// 二进制安全比较字符串(区分大小写)
$res = strcmp('hello', 'Hello');
// 输出1
echo $res;
以上代码输出如下:1