$x = NULL;

if ('0xFF' == 255) {

$x = (int)'0xFF';


This problem tests your knowledge of how type juggling, or coercion works.

When a string and a number are compared with the equality operator, the string is coerced to an integer. The string in the if statement of this problem is representing a hex number, and it happens to be equal to 255 in decimal, so they will compare as equal.

However, it is important to know that the typecast inside the if statement is handled differently. Internally, PHP uses a function called convert_to_long, which evaluates the string from left to right, and stops as soon as it hits a letter. So, the value in $x will be 0. If you don’t know how it works, you’ll wrongly assume that you’d get 255.

Coerced in two different ways