What read for PHP interview

Perhaps surprisingly, the answer is neither NULL nor 255. Rather, the answer is that will equal 0 (zero).

Why?

First, let’s consider whether will evaluate to true or false. When a hex string is loosely compared to an integer, it is converted to an integer. Internally, PHP uses to detect that the string contains a hex value and converts it to an integer (since the other operand is an integer). So in this case, ‘0xFF’ is converted to its integer equivalent which is 255. Since 255 = 255, this condition evaluates to true. (Note that this only works for hex strings, not for octal or binary strings.)

But if that’s the case, shouldn’t the statement execute and result in being set equal to 255?

Well, the statement does execute, but it results in being set equal to 0, not 255 (i.e., it is not set to the integer equivalent of ‘0xFF’). The reason is that the explicit type cast of the string to an integer uses (which works differently than the function that was used in evaluating the conditional expression, as explained above). processes the string one character at a time from left to right and stops at the first non-numeric character that it reaches. In the case of ‘0xFF’, the first non-numeric character is ‘x’, so the only part of the string processed is the initial ‘0’. As a result, the value returned by is 0, so when the code completes, will be equal to 0.