PHP determines whether strings are equal

(*-*)浩
Release: 2023-02-24 15:06:01
Original
5112 people have browsed it

PHP determines whether strings are equal

PHP string comparison functions mainly include strcmp, strcasecmp, strnatcmp, strnatcasecmp, and their usage is basically similar. (Recommended learning: PHP programming from entry to proficiency)

//按字节对字符串进行比较
int strcmp(string str1,string str2)
//同上,但是不区分大小写
int strcasecmp(string str1,string str2)
//按“自然排序”进行比较
int strnatcmp(string str1,string str2)
//同上,但是不区分大小写
int strnatcasecmp(string str1,string str2)
Copy after login

The return results of these four functions are the same

If str1 Equal to str2, return 0

If str1 is greater than str2, return 1

If str1 is less than str2, return -1

Natural sorting and dictionary sorting

Dictionary Sorting: Compare byte by byte according to byte ASCII

Natural sorting: According to human thinking, for example, "2">"11" in byte sorting, while "2"< "11"

Among the previous four comparison functions, you only need to understand what these two sortings are, and you can distinguish the four functions clearly. To give the most intuitive example: comparison of

hello11 ​​and hello2, in dictionary sorting, hello11

';          //-1
echo strcasecmp($str1,$str3).'
'; //0 echo strnatcmp($str1,$str2).'
'; //1 echo strnatcasecmp($str2,$str4).'
'; //0
Copy after login

The above is the detailed content of PHP determines whether strings are equal. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!