w3resource

PHP: is_numeric() function

Description

The is_numeric() function is used to check whether a variable is numeric or not.

Version:

(PHP 4 and above)

Syntax:

is_numeric (var_name)

Parameter:

Name Description Required /
Optional
Type
var_name The variable being checked Required Mixed*

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value:

TRUE if var_name is a number or a numeric string, FALSE otherwise.

Value Type: Boolean.

Example:

<?php
$var_name1=678;
$var_name2="a678";
$var_name3="678";
$var_name4="W3resource.com";
$var_name5=698.99;
$var_name6=array("a1","a2");
$var_name7=+125689.66;
if (is_numeric($var_name1))
{
echo "$var_name1 is Numeric.<br>" ;
}
else
{
echo "$var_name1 is not Numeric. <br>" ;
}
if (is_numeric($var_name2))
{
echo "$var_name2 is Numeric.<br>" ;
}
else
{
echo "$var_name2 is not Numeric.<br>" ;
}
$result=is_numeric($var_name3);
echo "[ $var_name3 is numeric? ]" .var_dump($result)."<br>";
$result=is_numeric($var_name4);
echo "[ $var_name4 is numeric? ]" .var_dump($result)."<br>";
$result=is_numeric($var_name5);
echo "[ $var_name5 is numeric? ]" .var_dump($result)."<br>";
$result=is_numeric($var_name6);
echo "[ $var_name6 is numeric? ]" .var_dump($result)."<br>";
$result=is_numeric($var_name7);
echo "[ $var_name7 is numeric? ]" .var_dump($result);
?>

Output:

678 is Numeric.
a678 is not Numeric.
bool(true)  [ 678 is numeric? ]
bool(false)  [ W3resource.com is numeric? ]
bool(true)  [ 698.99 is numeric? ]
bool(false)  [ Array is numeric? ]
bool(true)  [ 125689.66 is numeric? ]

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous: is_null
Next: is_object



PHP: Tips of the Day

SQL injection that gets around mysql_real_escape_string()

Consider the following query:

$iId = mysql_real_escape_string("1 OR 1=1");    
$sSql = "SELECT * FROM table WHERE id = $iId";

mysql_real_escape_string() will not protect you against this. The fact that you use single quotes (' ') around your variables inside your query is what protects you against this. The following is also an option:

$iId = (int)"1 OR 1=1";
$sSql = "SELECT * FROM table WHERE id = $iId";

Ref : https://bit.ly/32q3bJ7