is_resource()

Description

The is_resource() function is used to determine if a passed variable is a resource.

Syntax


bool is_resource( mixed $var )

Parameters

$var
Variable to test.

Return Value

boolean
A Boolean value is returned as follows:

  • true if passed variable is a resource
  • false if passed variable is not a resource

Examples

1. Check All Types with is_resource()

This example creates a variable of each data type and passes it to is_resource(). The return value of the is_resource() 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_resource( $boolVal ));
var_dump( is_resource( $intVal ));
var_dump( is_resource( $floatVal ));
var_dump( is_resource( $stringVal ));
var_dump( is_resource( $arrayVal ));
var_dump( is_resource( $objectVal ));
var_dump( is_resource( $resourceVal ));
var_dump( is_resource( $nullVal ));

fclose($resourceVal);
?>

The output from the program is shown below. Note that only the $resourceVal variable tests true as a resource.

bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
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_object
is_real
is_scalar
is_string
var_dump

[other is_* functions]
var_dump()

References