The comparison operators work for strings as well as numbers. PHP has automatic type conversions that can lead to counter intuitive results when the strings are interpretable as numbers. If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.
In the given example, we have taken three stings and perform comparison between them using comparison operator -
<?php
$str1 = "00004";
$str2 = "008";
$str3 = "00007-STR";
if($str2 < $str1) {
echo "$str1 is less than $str2<br/>";
}
if($str3 < $str2) {
echo "$str3 is less than $str2<br/>";
}
if($str1 < $str3) {
echo "$str1 is less than $str3<br/>";
}
?>
Output of the above code
00007-STR is less than 008 00004 is less than 00007-STR
Solution
The comparison operators work for strings as well as numbers. PHP has automatic type conversions that can lead to counter intuitive results when the strings are interpretable as numbers. If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.
In the given example, we have taken three stings and perform comparison between them using comparison operator -
Output of the above code
00007-STR is less than 008
need an explanation for this answer? contact us directly to get an explanation for this answer00004 is less than 00007-STR