PHP comparison operators appear to be too often, especially = =
if (a = = b) {
Do something
}
But have you really mastered the = =? The details are important!
Look at the code below and say what you think is the right answer.
Var_dump (' 123fg456 ' ==123);
Var_dump (' some string ' = 0);
Var_dump (123.0 = = ' 123d456 ');
Var_dump (0 = "a");
Var_dump ("1" = = "01");
Var_dump ("1" = = "1e0");
Think of your answer first, then run it again. If the answer is not wrong, then congratulations, the basic knowledge is very solid.
Explain:
If you compare an integer and a string, the string is converted to an integer. If two numeric strings are compared, they are compared as integers. This rule also applies to switch statements.
It is particularly noted that when a string is converted to an integer, it is left to right until a non-numeric character is encountered. That means ' 123abc456 ' will be converted to 123 instead of 123456. Another space that starts with a string is ignored, such as ' 234abc ' converted to 234.
Below are a list of loosely compared and strictly compared tables