Using PHP strcmp() Function: Syntax, Usage, and Examples
This article explains the PHP strcmp() function, covering its syntax, return values, and practical examples for comparing string equality, ordering, case sensitivity, and length, while also noting the related strcasecmp() function for case‑insensitive comparisons.
strcmp() Function Syntax
The strcmp() function in PHP compares two strings and returns an integer indicating their relationship.
<code>int strcmp ( string $str1 , string $str2 )</code>The function returns 0 if the strings are equal, a value greater than 0 if $str1 is greater than $str2 , and a value less than 0 if $str1 is less than $str2 .
Usage of strcmp()
1. Compare two strings for equality
You can use strcmp() to test whether two strings are identical. If they are, the function returns 0.
<code>$str1 = "Hello";
$str2 = "Hello";
$result = strcmp($str1, $str2);
if($result == 0) {
echo "两个字符串相等";
} else {
echo "两个字符串不相等";
}
</code>The above code outputs "两个字符串相等".
2. Compare two strings for ordering
Beyond equality, strcmp() can determine lexical ordering. It returns a positive value when $str1 is greater than $str2 , and a negative value when it is smaller.
<code>$str1 = "apple";
$str2 = "banana";
$result = strcmp($str1, $str2);
if($result > 0) {
echo "$str1 大于 $str2";
} elseif($result < 0) {
echo "$str1 小于 $str2";
} else {
echo "$str1 等于 $str2";
}
</code>The above code outputs "apple 小于 banana".
3. Case‑sensitive comparison
By default, strcmp() is case‑sensitive. To perform a case‑insensitive comparison, use strcasecmp() instead.
<code>$str1 = "Hello";
$str2 = "hello";
$result = strcasecmp($str1, $str2);
if($result == 0) {
echo "两个字符串相等";
} else {
echo "两个字符串不相等";
}
</code>The code prints "两个字符串相等" because strcasecmp() ignores case.
4. Compare string lengths
strcmp() can also be used to compare the lengths of strings, as longer strings are considered greater in lexical comparison.
<code>$str1 = "Hello";
$str2 = "Hello World";
$result = strcmp($str1, $str2);
if($result > 0) {
echo "$str1 长度大于 $str2";
} elseif($result < 0) {
echo "$str1 长度小于 $str2";
} else {
echo "$str1 长度等于 $str2";
}
</code>This code outputs "Hello 长度小于 Hello World".
Summary
The strcmp() function is a PHP built‑in used for string comparison. It can check equality, determine lexical ordering, and indirectly compare string lengths. Remember that it is case‑sensitive; for case‑insensitive checks, use strcasecmp() . Proper use of strcmp() enables effective string handling in PHP applications.
PHP Learning Recommendations
Vue3+Laravel8+Uniapp Beginner to Practical Development Tutorial
Vue3+TP6+API Social E‑commerce System Development Course
Swoole From Beginner to Master Course
Workerman+TP6 Real‑time Chat System Limited‑time Offer
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.