PHP – Determine Whether a Variable is an Object or an Array

This can be done easily in PHP. The language provides 2 functions which are useful in this case

is_object can detect if a variable is an object. It returns true if the given variable is an object.

$var = new stdClass();
  echo "Object";
} else {
  echo "Not an object";

is_array checks whether a variable is an array or not. It returns true if the given variable is an array, otherwise it returns false.

$var = array(0, 1);
if (is_array($var))
 echo 'This is an array....';
 echo 'This is not an array....';

PHP Readable var_dump is a small tool which can format display of an array or an object. It is not perfect but it can still show a nice output.

Leave a Comment

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

We care about your privacy and data security. Google serves cookies to analyse traffic and show ads on this site. By using this site you agree to these cookies being set. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.