In this post we will be creating PHP script that will check if an existing variable is an array, to do this we will be using the ‘is_array()’ function.
See our array of items for our first example below.
$array = array('James', 'Dustin', 'Billy', 'Amy', 'Shelly');
In the snippet of PHP code below we are applying the ‘is_array()’ function to check whether a variable is an array. Here we are also manipulating the output depending on weather True or False is returned.
<?php
$array = array('James', 'Dustin', 'Billy', 'Amy', 'Shelly');
echo is_array($array) ? '$array is array' : '$array is not Array';
?>
The above would return the following as output.
$array is an array
See below another example where our variable is clearly not an array.
<?php
$not_an_array = "Hello World";
echo is_array($not_an_array) ? '$not_an_array is an array' : '$not_an_array is not an Array';
?>
The above would return the following as output.
$not_an_array is not an Array
Take a look at some of our other content around the Python programming language by clicking here.