is_object()

Description

The is_object() function is used to determine if a passed variable is an object.

Syntax


bool is_object( mixed $var )

Parameters

$var
Variable to test.

Return Value

boolean
A Boolean value is returned as follows:

  • true if passed variable is an object
  • false if passed variable is not an object

Examples

1. Check All Types with is_object()

This example creates a variable of each data type and passes it to is_object(). The return value of the is_object() call is passed to var_dump() to display its type and value. Note that the file data.txt exists in the directory so the call to fopen() on line 8 succeeds.

<?php
$boolVal = true;
$intVal = 6;
$floatVal = 3.14;
$stringVal = "Hello";
$arrayVal = array( 1, "two", 3.4 );
$objectVal = new stdClass;
$resourceVal = fopen("./data.txt", "r");
$nullVal = null;

var_dump( is_object( $boolVal ));
var_dump( is_object( $intVal ));
var_dump( is_object( $floatVal ));
var_dump( is_object( $stringVal ));
var_dump( is_object( $arrayVal ));
var_dump( is_object( $objectVal ));
var_dump( is_object( $resourceVal ));
var_dump( is_object( $nullVal ));

fclose($resourceVal);
?>

The output from the program is shown below. Note that only the $objectVal variable tests true as an object.

bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
bool(false)
bool(false)

See Also

is_array
is_bool
is_callable
is_double
is_float
is_int
is_integer
is_long
is_null
is_numeric
is_real
is_resource
is_scalar
is_string
var_dump

[other is_* functions]
var_dump()

References