note 121886 added to language.types.integer
| From: | dhairyalakhera@php.net | Date: | Mon, 20 Nov 2017 16:04:49 +0000 |
| Subject: | note 121886 added to language.types.integer | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209176@lists.php.net to get a copy of this message | ||
-------------------------------------------------------------------------
Question :
var_dump((int) 010); //Output 8
var_dump((int) "010"); //output 10
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 ?
--------------------------------------------------------------------------
Answer :
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
----
Server IP: 208.43.231.9
Probable Submitter: 45.121.189.252
----
Manual Page -- http://php.net/manual/en/language.types.integer.php
Edit -- https://master.php.net/note/edit/121886
Del: integrated -- https://master.php.net/note/delete/121886/integrated
Del: useless -- https://master.php.net/note/delete/121886/useless
Del: bad code -- https://master.php.net/note/delete/121886/bad+code
Del: spam -- https://master.php.net/note/delete/121886/spam
Del: non-english -- https://master.php.net/note/delete/121886/non-english
Del: in docs -- https://master.php.net/note/delete/121886/in+docs
Del: other reasons-- https://master.php.net/note/delete/121886
Reject -- https://master.php.net/note/reject/121886
Search -- https://master.php.net/manage/user-notes.php