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

This can be done easily in PHP. The language provides 2 functions that 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 that can format the 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 *


Scroll to Top