|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-08-08 04:36 UTC] brucewlee at verizon dot net
Description:
------------
Neither is_numeric() nor intval() recognize '0b...' strings.
Test script:
---------------
Code:
$as = array("0x10", "16", "020", "0b10000");
foreach($as as $v) {
echo "The function is_numeric('" . $v . "') returns ";
if (is_numeric($v)) { echo("true"); } else { echo("false"); };
echo ".<br>";
}
Output:
The function is_numeric('0x10') returns true.
The function is_numeric('16') returns true.
The function is_numeric('020') returns true.
The function is_numeric('0b10000') returns false.
Code:
$as = array(["0x10", 16], ["16", 10], ["020", 8], ["0b10000", 2]);
foreach($as as $v) {
echo "The function intval('" . $v[0] . "', " . $v[1] . ") returns " . intval($v[0], $v[1]) . ".<br>";
}
Output:
The function intval('0x10', 16) returns 16.
The function intval('16', 10) returns 16.
The function intval('020', 8) returns 16.
The function intval('0b10000', 2) returns 0.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 02 13:00:01 2025 UTC |
This is actually a scripting engine problem. Both is_numeric() and intval() end up calling function that defined in Zend/zend_operators.c. Responsive functions are not treat binary strings as integer string. is_numeric() calls static inline zend_uchar is_numeric_string_ex(const char *str, int length, long *lval, double *dval, int allow_errors, int *oflow_info) inval() calls ZEND_API void convert_to_long_base(zval *op, int base) /* {{{ */ .... case IS_STRING: { char *strval = Z_STRVAL_P(op); Z_LVAL_P(op) = strtol(strval, NULL, base); STR_FREE(strval); } break;