In PHP, strcmp() is a commonly used string comparison function. It compares the size of two strings and returns different values based on the comparison. Understanding the meaning of strcmp() return values is very important when handling string operations in development.
The syntax of strcmp() 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 representing the comparison result between the two strings.
The integer returned by strcmp() usually has three possible outcomes: negative, zero, or positive.
When strcmp() returns 0, it means the two strings are exactly equal, including their content, case, and length. In short, the strings are identical in every way.
<span><span><span class="hljs-variable">$str1</span></span><span> = </span><span><span class="hljs-string">"hello"</span></span>;
</span><span><span class="hljs-variable">$str2</span></span><span> = </span><span><span class="hljs-string">"hello"</span></span>;
<p></span>if(strcmp($str1, $str2) == 0) {<br>
</span>echo </span>"The two strings are equal";<br>
}<br>
</span></span>
When strcmp() returns a negative value, it indicates that str1 is less than str2. This comparison is based on the 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" returns a negative value because the ASCII value of a is less than that of b.
<span><span><span class="hljs-variable">$str1</span></span><span> = </span><span><span class="hljs-string">"apple"</span></span>;
</span><span><span class="hljs-variable">$str2</span></span><span> = </span><span><span class="hljs-string">"banana"</span></span>;
<p></span>if(strcmp($str1, $str2) < 0) {<br>
</span>echo </span>"'apple' is less than 'banana'";<br>
}<br>
</span></span>
When strcmp() returns a positive value, it indicates that str1 is greater than str2. That is, str1 comes after str2 in dictionary order.
For example, comparing "banana" with "apple" returns a positive value because the ASCII value of b is greater than a.
<span><span><span class="hljs-variable">$str1</span></span><span> = </span><span><span class="hljs-string">"banana"</span></span>;
</span><span><span class="hljs-variable">$str2</span></span><span> = </span><span><span class="hljs-string">"apple"</span></span>;
<p></span>if(strcmp($str1, $str2) > 0) {<br>
</span>echo </span>"'banana' is greater than 'apple'";<br>
}<br>
</span></span>
It is important to note that strcmp() comparisons are case-sensitive. If you want a case-insensitive comparison, 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 class="hljs-variable">$str2</span></span><span> = </span><span><span class="hljs-string">"hello"</span></span>;
<p></span>if(strcmp($str1, $str2) != 0) {<br>
</span>echo </span>"The strings are different";<br>
}<br>
</span></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.