SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Whast the syntax for checking if a variable is a number?

    Like..
    if ($variable =~ s/[0-9]/g { good..all are numbers }
    else { fatal error..not numbers }

    sorry..just babbling some code....to give a better idea of what i'm saying..hehe


    Thanks!
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  2. #2
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found this code at a knowledge based site..after several hours..sigh ( inefficient )

    It is correct? I tested it, and seems to work..but just need counter check

    <?php

    $data = '12751345412154';
    if (eregi("[0-9]", $data)) {
    print "it is a number";
    }
    else { print "it is not a number"; }

    ?>
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  3. #3
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For PHP >= 4 use

    is_numeric()

    http://www.php.net/manual/en/function.is-numeric.php

    See also is_bool(), is_double(), is_float(), is_int(), is_real(), is_string(), is_object(), is_array(), and is_integer().

  4. #4
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow..thanks!
    less code to do the same function!

    $value = is_numeric ("$data");

    if ($value) { print " is a number"; }
    else { print "not a number"; }

    just that it requires >4.0
    yeah!

    thanks
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •