Comparison between PHP numbers and strings

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.