php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #77010 is_numeric with numeric strings suffixed with a string should return true
Submitted: 2018-10-13 20:46 UTC Modified: 2018-10-14 15:16 UTC
From: carusogabriel@php.net Assigned: carusogabriel (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: Next major version OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
37 + 25 = ?
Subscribe to this entry?

 
 [2018-10-13 20:46 UTC] carusogabriel@php.net
Description:
------------
As discussed in https://github.com/phpstan/phpstan/issues/1497, there're some strings containing castable integers, for example: https://3v4l.org/Ogral, that reports as false when passing it to is_numeric.

I'm opening this bug ticket to discuss if we need to consider this a bug and target all active branches, or consider as a Feature Request and propose this as a new feature to the next minor version of PHP.

Test script:
---------------
<?php

var_dump(is_numeric('2.14.0-DEV'));

Expected result:
----------------
bool(true)

Actual result:
--------------
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-10-13 20:49 UTC] requinix@php.net
"2.14.0-DEV" doesn't look like any number I've seen before.

So... what, if it starts with a digit then it's numeric? Should "1a" be numeric?
 [2018-10-13 20:56 UTC] carusogabriel@php.net
@requinix What do we consider as numbers, better, as numeric numbers in PHP?

I current judge everything that we can cast for an integer number, for example, as numeric numbers.
 [2018-10-13 21:00 UTC] requinix@php.net
Well, according to the docs,
> Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential
> part. Thus +0123.45e6 is a valid numeric value.

> I current judge everything that we can cast for an integer number, for example, as numeric numbers.
(int)"x" === 0. Does that mean "x" is numeric?
 [2018-10-13 21:05 UTC] carusogabriel@php.net
> Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential
> part. Thus +0123.45e6 is a valid numeric value.

Gotcha. So if we're going to take this further, we'd also consider a numeric string with a string suffixed, for example: "7.3.0-beta1".

> (int)"x" === 0. Does that mean "x" is numeric?

Let's pretend I've never used that argument :P
 [2018-10-13 21:06 UTC] carusogabriel@php.net
-Type: Bug +Type: Feature/Change Request -PHP Version: Irrelevant +PHP Version: Next major version
 [2018-10-13 21:08 UTC] carusogabriel@php.net
-Summary: is_numeric returns false for numeric strings +Summary: is_numeric with numeric strings suffixed with a string should return true
 [2018-10-13 21:24 UTC] requinix@php.net
> we'd also consider a numeric string with a string suffixed
That I can understand.

Supporting non-numeric suffixes will conflict with https://wiki.php.net/rfc/invalid_strings_in_arithmetic
so I think this would have to go to an RFC.
 [2018-10-14 15:16 UTC] carusogabriel@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: carusogabriel
 [2018-10-14 15:16 UTC] carusogabriel@php.net
> Supporting non-numeric suffixes will conflict with https://wiki.php.net/rfc/invalid_strings_in_arithmetic

Indeed, we had that RFC. I'll close this ticket and refactor the current implementation on that issue.

Thanks for the discussion.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 23:01:26 2024 UTC