In PHP, strcmp() is a commonly used string comparison function. It compares two strings and returns different values based on the comparison result. Understanding the meaning of strcmp() return values is essential for handling string operations in development.
The syntax of the strcmp() function is as follows:
<span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-title function_ invoke__">strcmp</span></span><span> ( </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$str1</span></span><span> , </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$str2</span></span><span> )
</span></span>
This function takes two parameters, str1 and str2, and returns an integer indicating the comparison result of the two strings.
The integer returned by strcmp() typically has three possible outcomes: negative, zero, or positive.
When strcmp() returns 0, it means the two strings are exactly equal, including content, case, and length. In short, the strings are identical literally.
<span><span><span class="hljs-variable">$str1</span></span><span> = </span><span><span class="hljs-string">"hello"</span></span><span>;
</span><span><span class="hljs-variable">$str2</span></span><span> = </span><span><span class="hljs-string">"hello"</span></span><span>;
<p></span>if (strcmp($str1, $str2) == 0) {<br>
echo "The two strings are equal";<br>
}<br>
</span>
When strcmp() returns a negative value, it means str1 is less than str2. The comparison is based on ASCII values of characters. In other words, if str1 comes before str2 in dictionary order, the return value will be negative.
For example, comparing "apple" with "banana", since the ASCII value of a is less than b, strcmp() returns a negative value.
<span><span><span class="hljs-variable">$str1</span></span><span> = </span><span><span class="hljs-string">"apple"</span></span><span>;
</span><span><span class="hljs-variable">$str2</span></span><span> = </span><span><span class="hljs-string">"banana"</span></span><span>;
<p></span>if (strcmp($str1, </span>$str2) < 0) {<br>
echo "'apple' is less than 'banana'";<br>
}<br>
</span>
When strcmp() returns a positive value, it means str1 is greater than str2. In other words, str1 comes after str2 in dictionary order.
For example, comparing "banana" with "apple", strcmp() returns a positive value because the ASCII value of b is greater than that of a.
<span><span><span class="hljs-variable">$str1</span></span><span> = </span><span><span class="hljs-string">"banana"</span></span><span>;
</span><span><span class="hljs-variable">$str2</span></span><span> = </span><span><span class="hljs-string">"apple"</span></span><span>;
<p></span>if (strcmp($str1, </span>$str2) > 0) {<br>
echo "'banana' is greater than 'apple'";<br>
}<br>
</span>
It is important to note that strcmp() performs a case-sensitive comparison. If you do not want case sensitivity, you can use functions like strcoll() or strcasecmp().
<span><span><span class="hljs-variable">$str1</span></span><span> = </span><span><span class="hljs-string">"Hello"</span></span><span>;
</span><span><span class="hljs-variable">$str2</span></span><span> = </span><span><span class="hljs-string">"hello"</span></span><span>;
<p></span>if (strcmp($str1, </span>$str2) != 0) {<br>
echo "Strings are different";<br>
}<br>
</span>
In this example, strcmp() will return a non-zero value because the ASCII value of 'H' is different from 'h'.
strcmp() is a powerful string comparison function that helps us understand the relative order of two strings by returning negative, zero, or positive values:
Returns 0: The two strings are equal.
Returns a negative value: str1 is less than str2.
Returns a positive value: str1 is greater than str2.
Understanding the return values of this function is very helpful for optimizing and debugging string operations.