is_numericCHECKS if a given
VARIABLE is a
NUMERIC VALUE or a
NUMERIC STRING.
This function returns a BOOLEAN value when it tests the following variable types:
STRING, NUMERIC, BOOLEAN, ARRAY, NULL, RESOURCE or OBJECT.
This functions returns TRUE wheather $var is a NUMERIC or FALSE in not.
To visualize the result, in this tutorial, we use var_dump associated with an if loop structure as a conditional evaluation framework.
<?php
bool is_numeric ( mix $var );
where,
mix $var = VARIABLE to test
?>
$var
The variable to test.
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - -
This is a simple example using
var_dump
to display the variable as is
- - - - - - - - - - - - - - - - - - - - - - - */
var_dump($_SERVER['REMOTE_ADDR']);
echo '<br><br>';
var_dump($_SERVER['HTTP_USER_AGENT']);
echo '<br><br>';
var_dump($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo '<br><br>';
var_dump(M_E);
echo '<br><br>';
$var01e = 1.6e-19;
var_dump($var01e);
$var01f = [ 'c' => 299792458, 'G' => 6.67428E-11 ];
echo '<br><br>';
var_dump($var01f);
$var01g = NULL;
echo '<br><br>';
var_dump($var01g);
?>
RESULT
string(3) "::1"
string(114) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36"
string(35) "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7"
float(2.718281828459)
float(1.6E-19)
array(2) { ["c"]=> int(299792458) ["G"]=> float(6.67428E-11) }
NULL
You may get some different results.
Everything will depend on the system involved.
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - -
This is a simple example using
if -> conditional evaluation
to display if the variable
is NUMERIC
- - - - - - - - - - - - - - - - - - - - - - - */
$var02a = $_SERVER['REMOTE_ADDR'];
$var02b = $_SERVER['HTTP_USER_AGENT'];
$var02c = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$var04 = M_E;
$var02d = 1.6e-19;
$var02e = [ 'c' => 299792458, 'G' => 6.67428E-11 ];
$var02f = NULL;
$var02g = '123456';
$var02h = '0x1e240';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_numeric($var02a))
{
var_dump($var02a);
echo '<br>is a NUMERIC value<br><br>';
}
else
{
var_dump($var02a);
echo '<br>is NOT a NUMERIC value<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_numeric($var02b))
{
var_dump($var02b);
echo '<br>is a NUMERIC value<br><br>';
}
else
{
var_dump($var02b);
echo '<br>is NOT a NUMERIC value<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_numeric($var02c))
{
var_dump($var02c);
echo '<br>is a NUMERIC value<br><br>';
}
else
{
var_dump($var02c);
echo '<br>is NOT a NUMERIC value<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_numeric($var04))
{
var_dump($var04);
echo '<br>is a NUMERIC value<br><br>';
}
else
{
var_dump($var04);
echo '<br>is NOT a NUMERIC value<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_numeric($var02d))
{
var_dump($var02d);
echo '<br>is a NUMERIC value<br><br>';
}
else
{
var_dump($var02d);
echo '<br>is NOT a NUMERIC value<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_numeric($var02e))
{
var_dump($var02e);
echo '<br>is a NUMERIC value<br><br>';
}
else
{
var_dump($var02e);
echo '<br>is NOT a NUMERIC value<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_numeric($var02f))
{
var_dump($var02f);
echo '<br>is a NUMERIC value<br><br>';
}
else
{
var_dump($var02f);
echo '<br>is NOT a NUMERIC value<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_numeric($var02g))
{
var_dump($var02g);
echo '<br>is a NUMERIC value<br><br>';
}
else
{
var_dump($var02g);
echo '<br>is NOT a NUMERIC value<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_numeric($var02h))
{
var_dump($var02h);
echo '<br>is a NUMERIC value<br><br>';
}
else
{
var_dump($var02h);
echo '<br>is NOT a NUMERIC value<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?>
RESULT
string(3) "::1"
is NOT a NUMERIC value
string(78) "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0"
is NOT a NUMERIC value
string(35) "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3"
is NOT a NUMERIC value
float(2.718281828459)
is a NUMERIC value
float(1.6E-19)
is a NUMERIC value
array(2) {
["c"]=>
int(299792458)
["G"]=>
float(6.67428E-11)
}
is NOT a NUMERIC value
NULL
is NOT a NUMERIC value
ATTENTION
string(6) "123456"
is a NUMERIC value
string(7) "0x1e240"
is NOT a NUMERIC value
"123456" = "0x1e240"
that is,
(123456)10 = (1e240)16
However,
"123456" is considered NUMERIC
and
"0x1e240" is a simple STRING.
This is true as of PHP 7.0.0
There is a simpler way of obtaining whether a variable is NUMERIC or not.
This will be seen the moment we study the user-defined functions.