Library code snippets

Determine if a variable is numeric

There are a number of ways to determine if a variable in PHP is numeric. If you have PHP 4, you can use the is_numeric function:

if (is_numeric($thefield)==false) {
  echo "\$thefield is not a number";
}

If you have PHP 3, then you can use the following function

function is_numeric($value) {
  if (((int) $value)== $value) {
    return true;
  } else {
    return false;
  }
}

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Determine if a variable is numeric.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...
AddThis

Related discussion

Related podcasts

  • EarthClassMail.com - Moving from LAMP to .NET 3.5

    Scott chats with Matt Davis, architect at EarthClassMail.com, about their move from a LAMP stack (Linux/Apache/mysql/PHP) to .NET 3.5. What's working, what's not, and what kinds of issues are they running into as their architect their solution.

Events coming up

  • Jul 18

    WordCamp UK 2009

    Cardiff, United Kingdom

    This is the second WordCamp UK - an informal annual gathering of WordPress publishers, designers and developers based in the United Kingdom.

We'd love to hear what you think! Submit ideas or give us feedback