is_boolFINDS if a given variable is a BOOLEAN.
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 BOOLEAN 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_bool ( mix $var );
where,
$var = VARIABLE to test
?>
$var
The variable to be checked.
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);
echo '<br><br>';
$var01i = true;
var_dump($var01i);
echo '<br><br>';
$var01j = FALSE;
var_dump($var01j);
?>
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)
bool(true)
bool(false)
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - -
This is a simple example using
if -> conditional evaluation
to display if the variable
is BOOLEAN
- - - - - - - - - - - - - - - - - - - - - - - */
$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;
$var02hf = false;
$var02hF = FALSE;
$var02it = true;
$var02iT = TRUE;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_bool($var02a))
{
var_dump($va01);
echo '<br>is a BOOLEAN<br><br>';
}
else
{
var_dump($var02a);
echo '<br>is NOT a BOOLEAN<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_bool($var02f))
{
var_dump($var02f);
echo '<br>is a BOOLEAN<br><br>';
}
else
{
var_dump($var02f);
echo '<br>is NOT a BOOLEAN<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_bool($var02e))
{
var_dump($var02e);
echo '<br>is a BOOLEAN<br><br>';
}
else
{
var_dump($var02e);
echo '<br>is NOT a BOOLEAN<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_bool($var02hf))
{
var_dump($var02hf);
echo '<br>is a BOOLEAN<br><br>';
}
else
{
var_dump($var02hf);
echo '<br>is NOT a BOOLEAN<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_bool($var02hF))
{
var_dump($var02hF);
echo '<br>is a BOOLEAN<br><br>';
}
else
{
var_dump($var02hF);
echo '<br>is NOT a BOOLEAN<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_bool($var02it))
{
var_dump($var02it);
echo '<br>is a BOOLEAN<br><br>';
}
else
{
var_dump($var02it);
echo '<br>is NOT a BOOLEAN<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_bool($var02iT))
{
var_dump($var02iT);
echo '<br>is a BOOLEAN<br><br>';
}
else
{
var_dump($var02iT);
echo '<br>is NOT a BOOLEAN<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?>
RESULT
string(3) "::1"
is NOT a BOOLEAN
array(2) {
["c"]=>
int(299792458)
["G"]=>
float(6.67428E-11)
}
is NOT a BOOLEAN
float(1.6E-19)
is NOT a BOOLEAN
bool(false)
is a BOOLEAN
bool(false)
is a BOOLEAN
bool(true)
is a BOOLEAN
bool(true)
is a BOOLEAN
There is a simpler way of obtaining whether a variable is a BOOLEAN or not.
This will be seen the moment we study the user-defined functions.