PHP is_object() Function

What is PHP is_object() Function?

If you want to know whether a variable is a object or not, use is_object() function.  there are only two Boolean values – TRUE and FALSE.

Syntax:

is_object(variable)

Parameters:

The Function has 1 parameter which is required-

variable (Required): It specifies a variable.

Return Values:

The function returns-

  • TRUE, if the variable is an object
  • FALSE, if the variable is an object

Examples:

Example 1:

<?php
class Car {
    public $brand;
}

$car_obj = new Car();

if (is_object($car_obj)) {
    echo "This is an object.";
} else {
    echo "This is not an object.";
}
?>

Output:

This is an object.

Example 2:

<?php
$var = 10;
echo is_object(10) ? "10 is an object." : "10 is not an object.";

?>

Output:

10 is not an object.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP is_object() Function

is_object() is a built-in variable handling function. it is an easy function to determine whether a variable is instance of an class or not.

Reference:

https://www.php.net/manual/en/function.is-object.php