$num = 0;if($num == 'hello'){ echo $num; // echo 0}
Reply content:
$num = 0;if($num == 'hello'){ echo $num; // echo 0}
Check the official website: PHP Type comparison table
Specific reasons, there are also instructions
Update 2015-12-10 09:54:28
Read your comments, I think it is necessary to update.
You made a mistake, and I call it the knowledge transfer for taking for granted . Look at the mystery of the Code series I wrote earlier.
String =true
0 = False
Your train of thought is this:
因为: 字符串=true (前提)因为:0 = false (前提)又因为: true != false (真理)所以: 字符串 != 0 (有依据吗??) [1]因为: 'hello' 是字符串 (前提)因为: $num = 0 (前提)所以: 'hello' != $num
Does it have a basis? seems to have.
If a=b,b=c, then a=c.
This is the transitivity that we often use,
Similarly: if a=b,b!=c, then a!=c.
There seems to be nothing wrong with it. That's why I call it the knowledge transfer for taking it for granted . PHP manual, never said the comparison operation conforms to transitivity , no, never.
Not just PHP, any language is, when you see A=B and b=c, do not take for granted that a=c.
-----------continue to be more 2015-12-10 10:08:30---------------
Off-topic.
Say a few other features.
In JavaScript, there is even code that does not satisfy the identity . What is identity, that is to say:
如果 A,那么 A=A
What about PHP, or continue to look at that picture:
We look at the diagonal. Wow, it's all TRUE
, thank goodness.
There is also an Exchange Law :
如果 A=B,那么 B=A
Look at the watch yourself.
==
when comparing numbers and strings, the string is converted to a number and then compared, and the rule for PHP to convert strings is to attempt to convert from the beginning of the string to an appropriate number, for example, to be converted to when the "123hello"
123
string starts with a number format 0
.
PHP is a weakly typed language, and when a number is compared to a string, the string is converted to a number and then compared, so 0 is compared to Hello, and he actually converts the hello to a number before the comparison, and turns out to be 0, so the two are equal with the = = comparison, If you want to strictly compare, you can use the = = = operator
Specific reference:
PHP type Comparison
PHP ==
type is different, it will be converted to the same type, the string after the write will be converted to numeric values, reference
http://php.net/manual/en/language.operators.comparison.php
' Hello ' because the first few characters do not contain numbers, so after conversion is 0, reference
Http://php.net/manual/en/language.types.string.php#language.types.string.conversion
This is not what you think the data all turn into a Boolean