strcmp (string compare) is a built-in PHP function used to compare two strings. It compares strings character by character until a difference is found. If the two strings are identical, strcmp returns 0; if the first string is greater than the second, it returns a positive integer; if the first string is smaller, it returns a negative integer.
Syntax:
<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><span class="hljs-keyword">int</span></span><span>
</span></span>
Parameters:
$str1: The first string to compare.
$str2: The second string to compare.
Return Values:
Returns 0 if the two strings are equal.
Returns a negative number if $str1 is less than $str2.
Returns a positive number if $str1 is greater than $str2.
Example:
<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>;
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">strcmp</span></span><span>(</span><span><span class="hljs-variable">$str1</span></span>, </span><span><span class="hljs-variable">$str2</span></span><span>); </span><span><span class="hljs-comment">// Outputs a negative number because "apple" < "banana"</span></span><span>
</span></span>
strcmp is case-sensitive, meaning that if two strings have the same letters but different cases, they are considered different.
Similar to strcmp, strcasecmp (string compare, case-insensitive) is also used to compare two strings. The difference is that strcasecmp ignores letter case when comparing strings. This makes it more flexible, especially when case-insensitive comparison is needed.
Syntax:
<span><span><span class="hljs-title function_ invoke__">strcasecmp</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><span class="hljs-keyword">int</span></span><span>
</span></span>
Parameters:
$str1: The first string to compare.
$str2: The second string to compare.
Return Values:
Returns 0 if the two strings are equal (case-insensitive).
Returns a negative number if $str1 is less than $str2.
Returns a positive number if $str1 is greater than $str2.
Example:
<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">"APPLE"</span></span><span>;
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">strcasecmp</span></span><span>(</span><span><span class="hljs-variable">$str1</span></span>, </span><span><span class="hljs-variable">$str2</span></span><span>); </span><span><span class="hljs-comment">// Outputs 0 because "apple" and "APPLE" are equal ignoring case</span></span><span>
</span></span>
The main difference between strcmp and strcasecmp is whether they are case-sensitive:
strcmp is case-sensitive and considers letter case in the comparison.
strcasecmp is case-insensitive and treats letters with different cases as equal.
Use strcmp when you need a strict case-sensitive comparison.
For example, in file systems, you may need to differentiate file names by case, especially on case-sensitive operating systems like Linux.
Use strcasecmp when you want a case-insensitive comparison.
For example, when handling user input where the case may vary, but you want the comparison to ignore case differences.
Example:
<span><span><span class="hljs-variable">$usernameInput</span></span><span> = </span><span><span class="hljs-string">"Admin"</span></span><span>;
</span><span><span class="hljs-variable">$usernameDatabase</span></span><span> = </span><span><span class="hljs-string">"admin"</span></span><span>;
<p></span>if (strcasecmp($usernameInput, $usernameDatabase) === 0) {<br>
echo "Username matches!";<br>
} else {<br>
echo "Username does not match!";<br>
}<br>
</span>
In this example, strcasecmp is used to compare the username input by the user with the username in the database. It treats uppercase and lowercase letters as equal.
strcmp and strcasecmp are both functions used to compare two strings, but their main difference lies in case sensitivity. strcmp performs a strict case-sensitive comparison, while strcasecmp ignores case. Choose the appropriate function based on your needs to get accurate comparison results. When handling sensitive data, case differences are usually important, whereas for user input or other non-sensitive data, a case-insensitive comparison is often more convenient.
Related Tags:
PDO