PHP strcmp関数:文字列比較の基本と実践

strcmp関数は、2つの文字列を比較するために使用され、2つの文字列の大小関係を表す整数値を返します。最初の文字列が2番めの文字列より小さい場合は負の数を返し、最初の文字列が2番めの文字列より大きい場合は正の数を返し、2つの文字列が等しい場合は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