Strange behavior: (0 == "string") true | switch (0) {} gives the first case "case" a ::

How will this happen?

var_dump(0=="some string"); // yields true, why? 

 switch(0) { case "a": echo "a"; // <-- we get here, why? break; case "b": echo "b"; break; default: echo "def"; break; } 

in accordance with this

 0=="some string" 0==(int)"some string" 0==0 true 

this is also logical:

  0=="some string" (string)0=="some string" "0"=="some string" false 
+4
source share
2 answers

Too long; Do not read

To summarize this rather long post; an implicit conversion from one type to another occurs; if you do not want this to happen, use a more strict === or an explicit cast.

Examples, including both, can be found later in this post.


Further reading about type manipulation can be found in the corresponding section of the manual located here .


Will the world end, do I need a PHP interpreter?

Not really, although it may seem unexpected at first sight, it is actually a function (type-juggling) of the language and is often used in other situations.

When comparing objects of different types, the PHP interpreter must find a common language, if it has different types, naturally, it will implicitly try to convert one into another.

In this case, it will try to convert the string "string" to a numeric value.

The following line is equivalent to what you have in your question:

 if (0 == intval ("string")) 

Hmm, wait .. erhm what !?

Since conversion from a "string" to a numerical value is actually impossible, an implicit conversion will yield 0 (as indicated by the language).

This is the string that will be converted to int, since this is what the PHP committee made the decision.

This is mainly because implicit conversion in this way will help with mathematical operations.

And this is a much more common expectation of such behavior than in the other around.

We know that a 0 to 0 comparison should compare true , and that’s exactly what happens. The following is equivalent to what you have in your question, although to explain the situation in more detail further.

 function equal_ ($lhs, $rhs) { if (gettype ($lhs) == 'integer') $rhs = intval($rhs); /* ... */ return $lhs === $rhs; } if (equal_ (0, "string")) { } 

But hey .. HEY, HOLD!

"When I use my switch, I don’t even call any comparison operator, what happens with this example?"

You do not, but the internal interpreters will execute your switch statement.

To work around this problem, you need to explicitly specify either the needle you are looking for or the values ​​used as labels so that they are of the same type.

Your previous snippet can be written as below to provide a more rigorous comparison.

 switch((string)0) { case "a": echo "a"; break; case "b": echo "b"; break; default: echo "def"; break; } 

What if I do not want to use this "function", what should I do?

Since PHP4 there is a more stringent comparison operator; === .

This will not allow the implicit conversion of one of the operands; instead, it will first check if the operands are of the same type - if not; it will return false.

Only if they are of the same type will it actually compare the two, the bottom function is largely equivalent to using === .

 function strict_equal_ ($lhs, $rhs) { if (gettype ($lhs) != gettype ($rhs)) return false; return $lhs == $rhs; } if (strict_equal_ (0, "string")) echo "True"; else echo "False"; 
+6
source

It is called the "type of juggling" , and it is a language function (as far as I know, every freely typed language). The comparison operator expects two values ​​of the same type. If not the same type, PHP tries to use both types of the most common types that are int here. It means

 0=="some string" 0==0 true 
+5
source

All Articles