Home  >  Article  >  Backend Development  >  PHP中strncmp()函数比较两个字符串前2个字符是否相等的方法_PHP

PHP中strncmp()函数比较两个字符串前2个字符是否相等的方法_PHP

WBOY
WBOYOriginal
2016-05-28 11:49:25976browse

本文实例讲述了PHP中strncmp()函数比较两个字符串前2个字符是否相等的方法。分享给大家供大家参考,具体如下:

PHP中的strncmp()函数用于比较两个字符串(区分大小写),可判断两个字符串前n个字符是否相等。

strncmp()函数定义如下:

strncmp(string1,string2,length)

参数说明:

string1 必需。规定要比较的首个字符串。
string2 必需。规定要比较的第二个字符串。
length 必需。规定比较中所用的每个字符串的字符数。

返回值说明:

如果两个字符串相等,则返回值为 0
如果 string1 小于 string2,则返回值小于0
如果 string1 大于 string2,则返回值大于0

示例代码如下:

<&#63;php
/* strncmp()函数比较两个字符串前n个字符是否相等
 *
 * 返回值:
 0 - 如果两个字符串相等
 <0 - 如果 string1 小于 string2
 >0 - 如果 string1 大于 string2
 */
 $str1="welcome to www.bitsCN.com";
 $str2="WELCOME TO WWW.BITSCN.COM";
 $str3="welcome to php";
 echo strncmp($str1,$str2,2);
 echo "<br/>";
 echo strncmp($str1,$str3,2);
&#63;>

运行结果如下:

1
0

更多关于PHP字符串操作相关内容可查看本站专题:《php字符串(string)用法总结》

希望本文所述对大家PHP程序设计有所帮助。

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