PHP – How to check if variable is an array?

PHP has an internal variables handling functions to finds whether a variables is an array

is_array

<?php
if( is_array($elements) ){
	//do your work
}
?>

Another fastest way of determining an array is by:
Cast the value to an array, then check using (===) if it is identical to the original.

<?php
if ( (array) $elements!== $elements) { 
     echo '$elements is not an array'; 
 } else { 
     echo '$elements is an array'; 
 } 
?>

By Keenlio, February 12, 2014

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *


8 × six =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>