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.

if(is_object($var)){
  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.

if (is_array($var))
 echo 'This is an array....';
else
 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 *

By continuing to use the site, you agree to the use of cookies. 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.

Close