Tiffany B. Brown

PHP Quickie: More on is_numeric vs. ctype_digit

UPDATE: Apparently there is a difference in the value of ctype_digit('') depending on your version of PHP. The example below held true for my 5.0.3 install. But when I tested this with PHP 5.1.4, I received the expected result. Both an empty string and a null value return false.

Has anyone else noticed this ctype_digit quirk? If so, what version of PHP are you using? (Thanks Vindicator, Tri Bui)

Just a quick follow-up to my post on is_numeric vs. ctype_digit. There is one quirk with ctype_digit that may affect your choice about whether to use it.

When the string in question is empty, ctype_digit returns TRUE. However, when it is null, ctype_digit will return FALSE. Try out the code below to see what I mean.

<?
$n = '';
$x = null;

var_dump(ctype_digit($n)); // returns bool(true)
var_dump(is_numeric($n)); // returns bool(false)
var_dump(ctype_digit($x)); // returns bool(false)
var_dump(is_numeric($x)); // returns bool(false)
>

11 Responses to “PHP Quickie: More on is_numeric vs. ctype_digit

  1. Tiffany Brown’s Blog: PHP Quickie: More on is_numeric vs. ctype_digit…

  2. Tri Bui says:

    Tiffany,

    For 4.4.2 I get the expected behavior.

    PHP 4.4.2 (cli) (built: Jun 16 2006 10:49:24)
    Copyright (c) 1997-2006 The PHP Group
    Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

    $ more t.php

    $ php t.php
    bool(false)
    bool(false)
    bool(false)
    bool(false)
    bool(false)
    bool(false)

    tri bui

  3. Tri Bui says:

    Tiffany,

    For 4.4.2 I get the expected behavior.

    PHP 4.4.2 (cli) (built: Jun 16 2006 10:49:24)
    Copyright (c) 1997-2006 The PHP Group
    Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

    $ more t.php

    $ php t.php
    bool(false)
    bool(false)
    bool(false)
    bool(false)
    bool(false)
    bool(false)

    tri bui

  4. Tri Bui says:

    My code got cut out.

    The extra checks were for $y = FALSE;

    tri bui

  5. Tri Bui says:

    My code got cut out.

    The extra checks were for $y = FALSE;

    tri bui

  6. Vindicator says:

    If using it on PHP 5.1.4 the results ctype_digit and is_nummeric are the same.

  7. Vindicator says:

    If using it on PHP 5.1.4 the results ctype_digit and is_nummeric are the same.

  8. Philip says:

    From the PHP Manual under ref.ctype:

    “When called with a string argument they will check every character in the string and will only return TRUE if every character in the string matches the requested criteria. When called with an empty string the result will always be TRUE in PHP

  9. Philip says:

    From the PHP Manual under ref.ctype:

    “When called with a string argument they will check every character in the string and will only return TRUE if every character in the string matches the requested criteria. When called with an empty string the result will always be TRUE in PHP

  10. Philip says:

    Your blog cut my post, here’s that last part:

    When called with an empty string the result will always be TRUE in PHP before 5.1 and FALSE since 5.1.

  11. Philip says:

    Your blog cut my post, here’s that last part:

    When called with an empty string the result will always be TRUE in PHP before 5.1 and FALSE since 5.1.