Odd 0 and null in PHP and Javascript

Source: Internet
Author: User

Observe the following PHP code:

If (''= false) echo 'OK'. PHP_EOL;
Else echo 'no'. PHP_EOL;
If ('') echo 'OK'. PHP_EOL;
Else echo 'no'. PHP_EOL;

If ('0' = false) echo 'OK'. PHP_EOL;
Else echo 'no'. PHP_EOL;
If ('0') echo 'OK'. PHP_EOL;
Else echo 'no'. PHP_EOL;

If (array () = false) echo 'OK'. PHP_EOL;
Else echo 'no'. PHP_EOL;
If (array () echo 'OK'. PHP_EOL;
Else echo 'no'. PHP_EOL;

If (array (0) = false) echo 'OK'. PHP_EOL;
Else echo 'no'. PHP_EOL;
If (array (0) echo 'OK'. PHP_EOL;
Else echo 'no'. PHP_EOL;

Check the following Javascript code:

If (''= false) console. log ('OK ');
Else console. log ('no ');
If ('') console. log ('OK ');
Else console. log ('no ');

If ('0' = false) console. log ('OK ');
Else console. log ('no ');
If ('0') console. log ('OK ');
Else console. log ('no ');

If ([] = false) console. log ('OK ');
Else console. log ('no ');
If ([]) console. log ('OK ');
Else console. log ('no ');

If ([0] = false) console. log ('OK ');
Else console. log ('no ');
If ([0]) console. log ('OK ');
Else console. log ('no ');

The output result of the above PHP code is:

OK
NO
OK
NO
OK
NO
NO
OK

The output result of the above Javascript code is:

OK
NO
OK
OK
OK
OK
OK
OK

After analysis, I found that when PHP checks whether an operand is true, it compares this operand with true for =, and array (0) = true; javascript does not = compare to determine whether an operand is true, but does it according to people's normal logic thinking. For example, we think that the string '0' contains characters and is not a Null String, so it is true. For example, most people think that an array is true, no matter whether there are any elements in the array. And array (0) = false in Javascript. This shows that the implementation mechanisms of PHP and Javascript are different during type conversion. Javascript is more user-friendly. Since the string '0' can be converted to 0 after type conversion, array [0] must be converted to 0 after type conversion, which is in line with the logic thinking of normal people. For PHP array (0) = true, remember it separately.

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.