int variable with leading zero?
but a leading 0 does indicate octal in many languages, as is the case here.
It does have to do with octal numbers, 042
is interpreted as the octal number 42
which is 4 * 8 + 2 = 34
.
Please be aware that the octal interpretation happens when the number literal is parsed while loading the PHP script. It has nothing to do with intval()
, which doesn't do anything here because the value is already integer.
Octal interpretation happens only with number literals, not when casting a string to integer:
intval(042) // == 34
intval('042') // == 42
(int)'042' // == 42
In PHP a number with leading 0 is read as an octal number.
So 042 is read as an octal number.
The intval() function converts it into decimal number, which is 34.
So the browser output is 34.