Binary‑Safe String Comparison with strcmp() in PHP
This article explains PHP's binary‑safe strcmp() function, detailing its case‑sensitive behavior, parameter meanings, return values, and provides a complete example demonstrating how to compare two strings and handle the result in code.
The strcmp() function in PHP performs a binary‑safe, case‑sensitive comparison between two strings and returns an integer indicating their lexical relationship.
Note: The comparison distinguishes between uppercase and lowercase characters.
Function signature
int strcmp(string $str1, string $str2)Parameters
str1 : The first string to compare.
str2 : The second string to compare.
Return values
If str1 is less than str2 , returns a value less than 0.
If str1 is greater than str2 , returns a value greater than 0.
If both strings are equal, returns 0.
Example usage
<?php
$var1 = "Hello";
$var2 = "hello";
if (strcmp($var1, $var2) !== 0) {
echo "$var1 is not equal to $var2 in a case sensitive string comparison";
} else {
echo "equal";
}
// Output: $var1 is not equal to $var2 in a case sensitive string comparisonLaravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
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.