php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61220 is_numeric returns FALSE for chr(0) and CHR(1) that are BIT, so NUMERIC.
Submitted: 2012-03-01 14:12 UTC Modified: 2012-03-02 00:55 UTC
Votes:2
Avg. Score:3.0 ± 2.0
Reproduced:1 of 2 (50.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: dosergio at ig dot com dot br Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: ALL
Private report: No CVE-ID: None
 [2012-03-01 14:12 UTC] dosergio at ig dot com dot br
Description:
------------
I think that is_numeric should return TRUE if you pass chr(0) or chr(1) to it because this chars are stored in database as BITs, that are numeric in the binary context. Would php adopt it ?
I had to use a strange conditional because of this supposed "bug":
[code]
if( is_numeric($myvar) || ord($myvar) < 2) { 
  # to check char 0 or 1 that are BITS, binary numeric.
}
[/code]

Test script:
---------------
if( is_numeric( chr(0)) || is_numeric(chr(1)) )
    echo "is_numeric is great!";
 else
    echo "is_numeric might be a little buggy";

Expected result:
----------------
is_numeric shoud recognize chr(0) and chr(1) as numeric, as they are chars for bit information.

Actual result:
--------------
is_numeric says chr(0) and chr(1) are both NOT numeric.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-01 14:13 UTC] dosergio at ig dot com dot br
if( is_numeric( chr(0)) && is_numeric(chr(1)) )  # <-- changed to &&
    echo "is_numeric is great!";
 else
    echo "is_numeric might be a little buggy";
 [2012-03-01 15:02 UTC] dosergio at ig dot com dot br
I also tested chr(0) and chr(1) with is_boolean and of course the function says it is not.
I think is_numeric and is_boolean should be revised to include 0, 1, chr(0) and chr(1) as valid numeric and valid booleans.
 [2012-03-01 15:45 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2012-03-01 15:45 UTC] rasmus@php.net
No, definitely not. chr(0) is a NUL which is distinct from the number 0 and 
chr(1) is a SOH character which has absolutely nothing to do with a number.
 [2012-03-01 16:26 UTC] dosergio at ig dot com dot br
Do you know the binary numeric system ?
Chr(0) is the octet 00000000 which value is 0, so it's NOT different from zero.
Chr(1) is the octet 00000001 which value is 1, NOT different from the numeric 1.

These two chars should ALSO be recognized as boolean in the "is_bool" function too. Everybody that program in C knows that 0 and 1 are the real booleans inside the engine.

If you are so sure, take the chance, and tell the MySQL team that they are wrong, because they are storing chr(0) and chr(1) for bit(1) values. 
Bit is defined as numeric data type in MySQL
 [2012-03-01 18:22 UTC] rasmus@php.net
Yes, binary is 0's and 1's at the low level. So is octal, decimal and hexadecimal 
or anything else you represent digitally. What matters here is the character 
representation of these numbers. The character representation of binary (as of 
PHP 5.4) is 0b0101 (for example). Just like 05 is octal, 5 is decimal and 0x05 is 
hexadecimal. All of these make is_numeric() return true.
 [2012-03-01 18:34 UTC] anon at anon dot anon
@dosergio

Chr is a PHP function that returns a string with the specified character code. Try this:

var_dump(chr(0));

It says:

string(1) " "

It's a string, and not a string containing a human-readable number, so is_numeric returns false as it says it will do in the documentation.

>numeric in the binary context

Everything is numeric in the "binary context". If is_numeric returned true for that reason, then it should be hard-wired to return true always, because you cannot possibly pass it an argument which is not made of bits.

>Tell the MySQL team that they are wrong, because they are storing chr(0) and chr(1) for bit(1) values.

I won't say that they're wrong, but they're definitely different to all other databases in this way. Read: http://www.xaprb.com/blog/2006/04/11/bit-values-in-mysql/

For some reason, new versions of MySQL return BIT columns to clients as strings instead of integers. To avoid the problems this causes, you could do one of the following:
(a) use an integer column instead (e.g., TINYINT(1) instead of a BIT(1))
(b) put +0 in the SQL expression, which forces the bit value to be returned as a number
(c) use the PHP ord() function to convert the character into its character code.

In any case, it's not a PHP bug.
 [2012-03-02 00:55 UTC] dosergio at ig dot com dot br
Thank you. You made me see that the problem is really with MySQL, not PHP.
I thought PHP x MySQL were totally compatible, but as we see, the bit data type is only bit for MySql, PHP sees it as non numeric string.
I imagined php could validate only chr(0) and chr(1) as valid numeric string exactly to make php even more glued with MySQL.
You are right about the binary context, and I suggested to have a special treatment only if the char was chr(0) and chr(1) not any others out of 48~57.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 23:01:30 2024 UTC