inconsistency in converting string to integer, when string is hex, prefixed with '0x'

梦想与她 提交于 2019-12-01 00:37:04

The direct casts (int)$str and (float)$str don't really work differently at all: They both read as many characters from the string as they can interpret as a number of the respective type.

For "0x4B0", the int-conversion reads "0" (OK), then "x" and stops, because it cannot convert "x" into an integer. Likewise for the float-conversion.

For "1.2e3", the int-conversion reads "1" (OK), then "." and stops. The float-conversion recognises the entire string as valid float notation.

The automatic type recognition for an expression like $str * 1 is simply more flexible than the explicit casts. The explicit casts require the integers and floats to be in the format produced by %i and %f in printf, essentially.

Perhaps you can use intval and floatval rather than explicit casts-to-int for more flexibility, though.

Finally, your question "is hexidecimal data supposed to be valid or invalid numeric data?" is awkward. There is no such thing as "hexadecimal data". Hexadecimal is just a number base. What you can do is take a string like "4B0" and use strtoul etc. to parse it as an integer in any number base between 2 and 36.[Sorry, that was BS. There's no strtoul in PHP. But intval has the equivalent functionality, see above.]

intval uses strtol which recognizes oct/hex prefixes when the base parameter is zero, so

var_dump(intval('0xef'));     // int(0)
var_dump(intval('0xff', 0));  // int(255)

Is there (or, rather, should there be) any relation between is_numeric() and the way how PHP treats strings when they are used as numbers?

There is no datatype called numeric in PHP, the is_numeric() function is more of a test for something that can be interpreted as number by PHP.

As far as such number interpreting is concerned, adding a + in front of the value will actually make PHP to convert it into a number:

$int = +'0x4B0';
$float = +'1.2e3';

You find this explained in the manual for string, look for the section String conversion to numbers.

As it's triggered by an operator, I don't see any need why there should be a function in PHP that does the same. That would be superfluous.


Internally PHP uses a function called zendi_convert_scalar_to_number for the add operator (assumable +) that will make use of is_numeric_string to obtain the number.

The exact same function is called internally by is_numeric() when used with strings.

So to trigger the native conversion function, I would just use the + operator. This will ensure that you'll get back the numeric pseudo-type (int or float).

Ref: /Zend/zend_operators.c; /ext/standard/type.c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!