In this article, you will learn how to check if a variable is an object of not. In PHP the is_object() function checks if a variable is an object. This function returns true (1) if the variable is an object, otherwise it returns false/nothing.
what is the syntax of the IS_OBJECT() function in php?
is_object(variable);
Parameters | Details |
---|---|
variable | Required – Specifies the variable to check |
examples of the IS_OBJECT() function
Example 1. In this example, we check whether a variable is an object or not.
<?php
function get_cars($obj) {
if (!is_object($obj)) {
return false;
}
return $obj->cars;
}
$obj = new stdClass();
$obj->cars = array("Volvo", "BMW", "Audi");
var_dump(get_cars(null));
echo "<br>";
var_dump(get_cars($obj));
?>