Dhairya Lakhera
Dhairya Lakhera

Reputation: 4808

Converting octal int to int is different from converting octal string to int

var_dump((int) 010);  //Output 8

var_dump((int) "010"); //output 10

I am unable to understand the PHP data type conversion method from string to integer.

First one is octal notation so the output is as I expect. But what about the when converting "010" to integer? I expect that it should also output 8.

How can I get 8 as the returned value from var_dump((int) "010");?

Upvotes: 0

Views: 1067

Answers (5)

T. AKROUT
T. AKROUT

Reputation: 1717

010 is considered as an octal number:

 010 = 0 x 8^0 + 1 x 8^1

For more information you can check intval documentation

Upvotes: 0

iainn
iainn

Reputation: 17417

Casting a string to an integer follows the same the logic used by the intval function:

Returns the integer value of var, using the specified base for the conversion (the default is base 10).

intval allows specifying a different base as the second argument, whereas a straight cast operation does not, so using (int) will always treat a string as being in base 10.

php > var_export((int) "010");
10
php > var_export(intval("010"));
10
php > var_export(intval("010", 8));
8

Upvotes: 3

rickdenhaan
rickdenhaan

Reputation: 11298

Casting to an integer using (int) will always cast to the default base, which is 10.

Casting a string to a number this way does not take into account the many ways of formatting an integer value in PHP (leading zero for base 8, leading "0x" for base 16, leading "0b" for base 2). It will simply look at the first characters in a string and convert them to a base 10 integer. Leading zeroes will be stripped off because they have no meaning in numerical values, so you will end up with the decimal value 10 for (int)"010".

Converting an integer value between bases using (int)010 will take into account the various ways of formatting an integer. A leading zero like in 010 means the number is in octal notation, using (int)010 will convert it to the decimal value 8 in base 10.

This is similar to how you use 0x10 to write in hexadecimal (base 16) notation. Using (int)0x10 will convert that to the base 10 decimal value 16, whereas using (int)"0x10" will end up with the decimal value 0: since the "x" is not a numerical value, anything after that will be ignored.

If you want to interpret the string "010" as an octal value, you need to instruct PHP to do so. intval("010", 8) will interpret the number in base 8 instead of the default base 10, and you will end up with the decimal value 8. You could also use octdec("010") to convert the octal string to the decimal value 8. Another option is to use base_convert("010", 8, 10) to explicitly convert the number "010" from base 8 to base 10, however this function will return the string "8" instead of the integer 8.

Upvotes: 5

Cédric
Cédric

Reputation: 567

Integers starting with 0 are in octal notation.

Same thing for integers starting with 0x (eg 0x2A) that are in hexadecimal notation and integers starting with 0b that are in binary notation (eg 0b101010).

Relevant documentation: http://php.net/manual/language.types.integer.php

Upvotes: 1

Taminoful
Taminoful

Reputation: 78

PHP handles numers starting with a zero as octal, so this result is absolutly right.
You should look up PHP Integers for more information about this topic.

Upvotes: -1

Related Questions