Have played with hex values in PHP, but haven't understand this behavior yet
echo hexdec(0x80); //296
echo hexdec((string)0x80); //296
echo hexdec("0x80"); //128
Have played with hex values in PHP, but haven't understand this behavior yet
echo hexdec(0x80); //296
echo hexdec((string)0x80); //296
echo hexdec("0x80"); //128
hexdec()
assumes its’s getting a string. In the first you’re taking a number 0x80
, which is 128
in decimal, and giving it to it. This means it needs to be made into a string “128”
because the default way to convert is the decimal form. When you then want that to be treated as hex it will become 296
.
In the last one you actually say “here’s a string of a hex number, convert it to decimal” and the conversion is done.
Numeric literals aren’t handled as hex or dec or anything. They’re just numbers and hexdec()
isn’t meant to be used with them. Only strings. This is where implicit conversions between types may cause issues.