简体   繁体   中英

php converting string to integer unexpected behaviour

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

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

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

First one is octal notation so the output is correct. But what about the when converting "010" to integer. it should be also output 8.

Below all the answer is for var_dump((int) 010); , but i want answer for var_dump((int) "010");

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 .

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

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

010 is considered as an octal number:

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

For more information you can check intval documentation

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM