Bug #66399 [Opn]: Inconsistent runtime support for integer binary/hex syntax
| From: | krakjoe@php.net | Date: | Fri, 03 Jan 2014 13:52:39 +0000 |
| Subject: | Bug #66399 [Opn]: Inconsistent runtime support for integer binary/hex syntax | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-183560@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66399&edit=1
ID: 66399
Updated by: krakjoe@php.net
Reported by: benjamin at zikarsky dot de
Summary: Inconsistent runtime support for integer binary/hex
syntax
Status: Open
Type: Bug
Package: Strings related
PHP Version: 5.5.7
-Assigned To:
+Assigned To: dmitry
Block user comment: N
Private report: N
New Comment:
Dmitry, assigning to you to get some attention, this appears quite important to me. I'm not
able to say if the patch has adverse performance impact or introduces some strangeness you purposely
left out.
It looks good to me ?
Previous Comments:
------------------------------------------------------------------------
[2014-01-03 13:42:59] krakjoe@php.net
All Zend and ext/standard/string tests passing ...
------------------------------------------------------------------------
[2014-01-03 13:41:17] krakjoe@php.net
Patch attached ... while this has the desired outcome, I've not tested and don't know if
it introduces inconsistencies elsewhere.
I would assume there is some non-obvious reason this wasn't done in the first place ?
<?php
$bin = "0b100";
$hex = "0x04";
var_dump((int) $bin, (int) $hex);
var_dump((double) $bin, (double) $hex);
var_dump(is_numeric($bin));
var_dump(is_numeric($hex));
var_dump(intval($bin), intval($hex));
var_dump(floatval($bin), floatval($hex));
Yields:
int(4)
int(4)
float(4)
float(4)
bool(true)
bool(true)
int(4)
int(4)
float(4)
float(4)
------------------------------------------------------------------------
[2014-01-03 13:39:44] krakjoe@php.net
The following patch has been added/updated:
Patch Name: inconsistent-bin-hex-handling.patch
Revision: 1388756384
URL: https://bugs.php.net/patch-display.php?bug=66399&patch=inconsistent-bin-hex-handling.patch&revision=1388756384
------------------------------------------------------------------------
[2014-01-03 11:46:47] benjamin at zikarsky dot de
Description:
------------
While PHP's support for hexadecimal and binary (>=5.4) integer literals works fine at
"compile"-time, it is inconsistent/lacking at runtime. E.g. in strings.
- is_numeric($v) detects hexadecimal format but not the binary version
- casting to int (both with (int) and intval($v)) is not supported for either format
I am not sure if this is a bug/missing feature or unwanted behavior. I think it should be at least
consistent for is_numeric.
Test script:
---------------
<?php
$bin = "0b100";
$hex = "0x04";
var_dump((int) $bin);
var_dump((int) $hex);
var_dump(is_numeric($bin));
var_dump(is_numeric($hex));
var_dump(intval($bin));
var_dump(intval($hex));
Expected result:
----------------
int(4)
int(4)
bool(true)
bool(true)
int(4)
int(4)
Actual result:
--------------
Tested with:
PHP 5.5.7 (cli) (built: Jan 3 2014 11:33:48)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
Output:
int(0)
int(0)
bool(false)
bool(true)
int(0)
int(0)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66399&edit=1