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.