<?php var_dump('0xD' * 1 ); var_dump((int)'0xD'); var_dump(intval('0xD')); var_dump((float)'0xD');
int(13) int(0) int(0) float(0)
Why result of first two conditions are not same? Can you provide me correct documentation?
All that I found is
- Type Casting: Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast. It may not be obvious exactly what will happen when casting between certain types. For more information, see these sections:…
- Converting to integer: From strings:
See String conversion to numbers
- String conversion to numbers When a string is evaluated in a numeric context, the resulting value and type are determined as follows.
So, As far as I understand, docs say that casting to int directly and by putting in numeric context should be same. What is my mistake?
ADDED: Try to check first and second code (and outputs) before answering.
'0xD' is a string. The documentation clearly specifies how a string is “casted” into an integer value (picking the
(int) '0xD'; example here):
When a string is evaluated in a numeric context, the resulting value and type are determined as follows.
If the string does not contain any of the characters ‘.’, ‘e’, or ‘E’ and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.
The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an ‘e’ or ‘E’ followed by one or more digits.
The initial porition of the string
'0xD' that is a number is
0, hence the it’s
Don’t mix this with writing code. If you write that in code it’s not a string, it’s the hexa-decimal representation/notation of an integer number (Demo):
<?php $value = eval("return 0xD;"); var_dump($value); # int(13)
This does not fully answer why the expression
'0xD' * 1 results in
(int) 13, but explains everything else so far and which two rules of integer value interpretation apply in PHP.
I assume that there is a slight difference between casting to integer and integer context in PHP. If the string representing a hexadecimal number it is used in integer/float context:
'0xD' * 1
In this expression, the string
'0xD' will get evaluated as number because of the
* operator and the
1 operand (see Type Juggling). This is not Type Casting, because there is no cast language construct.
It looks like that in this expression’s case, the interpretation of the string as an integer is done literally (as specified) but is not the same as when you do a conversion.
One could argue it remains undocumented when conversion applies and when literal interpretation applies. However, using type casting can get you around any such problems (Demo):
<?php $value = '0xD'; var_dump($value); # string(3) "0xD" var_dump($value * 1); # int(13) var_dump((int) $value * 1); # int(0)
It’s the quotes, apparantly.
<?php var_dump('0xD' * 1 ); var_dump((int) 0xD); var_dump(intval(0xD)); var_dump((float) 0xD);
int(13) int(13) int(13) float(13)
I think PHP does not see the hex values in the strings as possible ints.