Int variable with initial zero?

Why does this lead to the following results in 34? It seems to have nothing to do with octal numbers.

intval(042); 
+7
source share
5 answers

but the leading 0 indicates octality in many languages, as is the case here.

+15
source

It relates to octal numbers, 042 interpreted as the octal number 42 , which is 4 * 8 + 2 = 34 .

Remember that octal interpretation occurs when a numeric letter is parsed when loading a PHP script. It has nothing to do with intval() , which does nothing here, because the value is already an integer.

Octal interpretation occurs only with numeric literals, and not when casting a string to an integer:

 intval(042) // == 34 intval('042') // == 42 (int)'042' // == 42 
+1
source

In PHP, a number with a leading 0 is read as an octal number.

So, 042 reads like an octal number.

The intval () function converts it to a decimal number, which is 34.

Thus, the browser output is 34.

+1
source

This is just a function definition. The leading zero is a command that analyzes it as an octal number, similar to how 0x means hex as a prefix. See the documentation for more details.

0
source

Be careful when passing this function to a string value with a leading "0". If you give it “042”, then it will treat it as BASE 8-9 and convert it to the decimal value, which is based by default.

Pass this

0
source

All Articles