is_numeric()

Description

This function determines whether a passed expression is numeric. A value is considered numeric if it contains an optional sign, followed by one or more digits, followed by an optional decimal part, and an optional exponential part.

Syntax


bool is_numeric( mixed $expr )

Parameters

$expr
The expression to test

Return Value

bool
The function will return one of the following values:

  • true if the passed $expr is a valid number
  • false if the passed $expr is not a valid number

Examples

1. Numbers and strings in Various Formats

This example tests numeric and numeric string expressions in various formats.

***Note: need to re-run with PHP 5.4***

<?php
echo '123'          . ( is_numeric( 123 )          ? " is" : " is not" ) . " numeric\n";
echo '"123"'        . ( is_numeric( "123" )        ? " is" : " is not" ) . " numeric\n";
echo '"123.45"'     . ( is_numeric( "123.45" )     ? " is" : " is not" ) . " numeric\n";
echo '"123.45e3"'   . ( is_numeric( "123.45e3" )   ? " is" : " is not" ) . " numeric\n";
echo '"-123.45e3"'  . ( is_numeric( "-123.45e3" )  ? " is" : " is not" ) . " numeric\n";
echo '"$123.45"'    . ( is_numeric( "$123.45" )    ? " is" : " is not" ) . " numeric\n";
echo '"0123"'       . ( is_numeric( "0123" )       ? " is" : " is not" ) . " numeric\n";
echo '"0x123"'      . ( is_numeric( "0x123" )      ? " is" : " is not" ) . " numeric\n";
echo '"0b01101010"' . ( is_numeric( "0b01101010" ) ? " is" : " is not" ) . " numeric\n";  // PHP 5.4
echo '"Hello123"'   . ( is_numeric( "Hello123" )   ? " is" : " is not" ) . " numeric\n";
echo '"123Hello"'   . ( is_numeric( "123Hello" )   ? " is" : " is not" ) . " numeric\n";
?>
123 is numeric
"123" is numeric
"123.45" is numeric
"123.45e3" is numeric
"-123.45e3" is numeric
"$123.45" is not numeric
"0123" is numeric
"0x123" is numeric
"0b01101010" is not numeric
"Hello123" is not numeric
"123Hello" is not numeric

2. Example heading

Description of example….



See Also

References