php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #73374 intval() with base 0 should detect binary
Submitted: 2016-10-22 21:58 UTC Modified: 2016-10-31 17:41 UTC
From: nikic@php.net Assigned:
Status: Closed Package: Math related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2016-10-22 21:58 UTC] nikic@php.net
Description:
------------
intval() using base 0 currently detects decimal, octal and hexadecimal numbers based on notation. However, binary numbers are always converted to 0. As PHP supports binary number literals as of PHP 5.4 they should be supported here as well.

https://3v4l.org/GYVQa

Test script:
---------------
var_dump(intval('0b1101', 0));

Expected result:
----------------
int(13)

Actual result:
--------------
int(0)

Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-22 23:00 UTC] ajf@php.net
-Type: Bug +Type: Feature/Change Request
 [2016-10-22 23:00 UTC] ajf@php.net
I'm recategorising this as a feature request, because intval() isn't inherently tied to PHP's integer literal formats, much as one might wish it were.
 [2016-10-25 15:00 UTC] cmb@php.net
-Summary: intval() with base 0 does not detect binary +Summary: intval() with base 0 should detect binary
 [2016-10-25 15:00 UTC] cmb@php.net
Also, the behavior is documented as such[1]:

| If base is 0, the base used is determined by the format of var:
| * if string includes a "0x" (or "0X") prefix, the base is taken as 16 (hex); otherwise,
| * if string starts with "0", the base is taken as 8 (octal); otherwise,
| * the base is taken as 10 (decimal).

[1] <http://php.net/manual/en/function.intval.php>
 [2016-10-31 17:41 UTC] leigh@php.net
Added a PR for this, feels clunky to me but not sure how to avoid it, what do you think?
 [2017-01-12 17:54 UTC] nikic@php.net
Automatic comment on behalf of leigh
Revision: http://git.php.net/?p=php-src.git;a=commit;h=4f7b498d12b04ae25ae59a711e76410e77455a94
Log: Fixed bug #73374
 [2017-01-12 17:54 UTC] nikic@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC