is_floatCHECK if a given VARIABLE is or is not is a floating point
DECIMAL VALUE.
Aliases:
is_double and
is_real.
is_double
CHECK if a given VARIABLE is or is not is a floating point DECIMAL VALUE.
Aliases: is_float, is_real.
is_real
CHECK if a given VARIABLE is or is not is a floating point DECIMAL VALUE.
Aliases: is_float, is_double.
This function returns a BOOLEAN value when it tests the following variable types:
STRING, NUMERIC, BOOLEAN, ARRAY, NULL, RESOURCE or OBJECT.
One way to visualize the test result is to use var_dump, however, it is better to use if as a conditional evaluation framework.
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Aliases:
is_float, is_double and is_real
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
bool is_float ( mix $var );
or
bool is_double ( mix $var );
or
bool is_real ( mix $var );
where,
$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);
echo '<br><br>';
$var01h = 123456;
var_dump($var01h);
?>
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
int(123456)
For you, some STRING results may be different.
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - -
This is a simple example using
if -> conditional evaluation
to display if the variable
is a FLOAT NUMBER
The same as: is_double or is_real
- - - - - - - - - - - - - - - - - - - - - - - */
$var02a = $_SERVER['REMOTE_ADDR'];
$var02b = $_SERVER['HTTP_USER_AGENT'];
$var02c = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$var02d = M_E;
$var02e = 1.6e-19;
$var02f = [ 'c' => 299792458, 'G' => 6.67428E-11 ];
$var02g = NULL;
$var02h = 123456;
$var02i = '123456';
$var02j = '0x1E240';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_float($var02a))
{
// if(is_double($var02a))
// if(is_real($var02a))
var_dump($var02a);
echo '<br>is a FLOAT<br><br>';
}
else
{
var_dump($var02a);
echo '<br>is NOT a FLOAT<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_float($var02b))
{
// if(is_double($var02b))
// if(is_real($var02b))
var_dump($var02b);
echo '<br>is a FLOAT<br><br>';
}
else
{
var_dump($var02b);
echo '<br>is NOT a FLOAT<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_float($var02c))
{
// if(is_double($var02c))
// if(is_real($var02c))
var_dump($var02c);
echo '<br>is a FLOAT<br><br>';
}
else
{
var_dump($var02c);
echo '<br>is NOT a FLOAT<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_float($var02d))
{
// if(is_double($var02d))
// if(is_real($var02d))
var_dump($var02d);
echo '<br>is a FLOAT<br><br>';
}
else
{
var_dump($var02d);
echo '<br>is NOT a FLOAT<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_float($var02e))
{
// if(is_double($var02e))
// if(is_real($var02e))
var_dump($var02e);
echo '<br>is a FLOAT<br><br>';
}
else
{
var_dump($var02e);
echo '<br>is NOT a FLOAT<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_float($var02f))
{
// if(is_double($var02f))
// if(is_real($var02f))
var_dump($var02f);
echo '<br>is a FLOAT<br><br>';
}
else
{
var_dump($var02f);
echo '<br>is NOT a FLOAT<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_float($var02g))
{
// if(is_double($var02g))
// if(is_real($var02g))
var_dump($var02g);
echo '<br>is a FLOAT<br><br>';
}
else
{
var_dump($var02g);
echo '<br>is NOT a FLOAT<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_float($var02h))
{
// if(is_double($var02h))
// if(is_real($var02h))
var_dump($var02h);
echo '<br>is a FLOAT<br><br>';
}
else
{
var_dump($var02h);
echo '<br>is NOT a FLOAT<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_float($var02i))
{
// if(is_double($var02i))
// if(is_real($var02i))
var_dump($var02i);
echo '<br>is a FLOAT<br><br>';
}
else
{
var_dump($var02i);
echo '<br>is NOT a FLOAT<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_float($var02j))
{
// if(is_double($var02j))
// if(is_real($var02j))
var_dump($var02j);
echo '<br>is a FLOAT<br><br>';
}
else
{
var_dump($var02j);
echo '<br>is NOT a FLOAT<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?>
RESULT
string(3) "::1"
is NOT a FLOAT
string(78) "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0"
is NOT a FLOAT
string(35) "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3"
is NOT a FLOAT
float(2.718281828459)
is a FLOAT
float(1.6E-19)
is a FLOAT
array(2) {
["c"]=>
int(299792458)
["G"]=>
float(6.67428E-11)
}
is NOT a FLOAT
NULL
is NOT a FLOAT
int(123456)
is NOT a FLOAT
string(6) "123456"
is NOT a FLOAT
string(7) "0x1E240"
is NOT a FLOAT
There is a simpler way of obtaining whether a variable is a FLOAT or not.
This will be seen the moment we study the user-defined functions.