PHP double equals vs. triple equals on a number and string
During development of a new, yet to be named, client site we're passing JSON to PHP and converting the JSON to XML for passing off to a final API endpoint. My initial PHP didn't have support for attributes, so I figured I would just go on and add it. Simple enough. So I made a special case that says: If you see a key that is equal to the string "@attributes" don't create a node, it should be processed as an attribute list. However, I ran into an issue that stumped me for a bit. It was skipping the entire JSON.
Can you see the bug in this code:
It turns out that if $key is a number it will pass the condition.
Run this code:
Expected:
NO
NO
Result:
YES
NO
Strangely enough this only seems to work with zero. I tried "0" (with quotes) and other numbers, but the result is always the expected one "NO NO". There's a reason why developers always recommend using triple equals.


