1 <? PHP 2/* comparison test of numbers and strings */3 // comparison between numbers 0 and null strings 4 If (0 = '') 5 echo "0 ='' <br/> "; // OK 6 7 // compare the number 0 with the string '0' 8 if (0 = '0 ') 9 echo "0 = '0' <br/>"; // ok10 11 // number 0 and string '0. 0' compare 12 if (0 = '0. 0') 13 echo "0 = '0. 0' <br/> "; // ok14 15 // The number 0 compares with the string '00' 16 if (0 = '00 ') 17 echo "0 = '00' <br/>"; // ok18 19 // The number 0 compares with the string '1' 20 if (0 = '1 ') 21 echo "0 = '1' <br/>"; // not ok22 23 // compare the number 0 with the string 'A' 24 if (0 = 'A ') 25 Echo "0 = 'A' <br/>"; // OK !!! 26 27 // compare the number 0 with the string 'B' 28 If (0 = 'B') 29 echo "0 = 'B' <br/> "; // OK !!! 30 31 // number 0 and string 'as8dfu9asfosj24279sjdf 'comparison 32 If (0 = 'hangzhou') 33 echo "0 = 'as8dfu9asfofosj24279sjdf' <br/> "; // OK !!! 34 35 // compare the number 0 with the string '2a '36 IF (0 = '2a') 37 echo "0 = '2a '<br/> "; // not ok38 39 // The number 0 is compared with the string '0a' 40 if (0 = '0a') 41 echo "0 = '0a' <br/> "; // ok42?>
Explanation
<?php echo (float)'a' . '<br/>'; // 0 echo (float)'2a' . '<br/>'; // 2 echo (float)'a2a' . '<br/>'; // 0 echo (float)'22a' . '<br/>'; // 22?>
PHP automatically converts strings containing letters to 0 before comparison,
For a string starting with a number, it is automatically converted into a corresponding number before comparison.