PHP strcmp: String Comparison Guide

The strcmp function is used to compare two strings and will return an integer value indicating their size relation. If the first string is smaller than the second string, it returns a negative number; if the first string is greater than the second string, it returns a positive number; if the two strings are equal, it returns 0.

$string1 = "hello";
$string2 = "world";

$result = strcmp($string1, $string2);

if($result < 0) {
    echo "String 1 is less than String 2";
} elseif($result > 0) {
    echo "String 1 is greater than String 2";
} else {
    echo "String 1 is equal to String 2";
}
bannerAds