is_arrayCHECK if a given VARIABLE is or is not an
ARRAY.
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 an ARRAY 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_array ( 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>';
$var01f = false;
$var01F = FALSE;
$var01t = true;
$var01T = TRUE;
var_dump($var01f);
echo '<br><br>';
var_dump($var01t);
echo '<br><br>';
?>
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
bool(false)
bool(true)
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - -
This is a simple example using
if -> conditional evaluation
to display if the variable
is ARRAY
- - - - - - - - - - - - - - - - - - - - - - - */
$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_array($var02a))
{
print_r($var02a);
echo '<br>is an ARRAY!<br><br>';
}
else
{
var_dump($var02a);
echo '<br>is NOT an ARRAY!<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_array($var02b))
{
print_r($var02b);
echo '<br>is an ARRAY!<br><br>';
}
else
{
var_dump($var02b);
echo '<br>is NOT an ARRAY!<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_array($var02c))
{
print_r($var02c);
echo '<br>is an ARRAY!<br><br>';
}
else
{
var_dump($var02c);
echo '<br>is NOT an ARRAY!<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_array($var02d))
{
print_r($var02d);
echo '<br>is an ARRAY!<br><br>';
}
else
{
var_dump($var02d);
echo '<br>is NOT an ARRAY!<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_array($var02e))
{
print_r($var02e);
echo '<br>is an ARRAY!<br><br>';
}
else
{
var_dump($var02e);
echo '<br>is NOT an ARRAY!<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_array($var02f))
{
print_r($var02f);
echo '<br>is an ARRAY!<br><br>';
}
else
{
var_dump($var02f);
echo '<br>is NOT an ARRAY!<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_array($var02g))
{
print_r($var02g);
echo '<br>is an ARRAY!<br><br>';
}
else
{
var_dump($var02g);
echo '<br>is NOT an ARRAY!<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_array($var02hf))
{
print_r($var02hf);
echo '<br>is an ARRAY!<br><br>';
}
else
{
var_dump($var02hf);
echo '<br>is NOT an ARRAY!<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_array($var02iT))
{
print_r($var02iT);
echo '<br>is an ARRAY!<br><br>';
}
else
{
var_dump($var02iT);
echo '<br>is NOT an ARRAY!<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?>
RESULT
string(3) "::1"
is NOT an ARRAY!
string(78) "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0"
is NOT an ARRAY!
string(35) "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3"
is NOT an ARRAY!
float(2.718281828459)
is NOT an ARRAY!
float(1.6E-19)
is NOT an ARRAY!
Array
(
[c] => 299792458
[G] => 6.67428E-11
)
is an ARRAY!
NULL
is NOT an ARRAY!
bool(false)
is NOT an ARRAY!
bool(true)
is NOT an ARRAY!
There is a simpler way of obtaining whether a variable is an ARRAY or not.
This will be seen the moment we study the user-defined functions.