PHP Function Reference

PHP is_resource() Function



The PHP is_resource() function checks whether a variable is a resource or not. The function returns true if the variable is a resource, otherwise it returns false.

Note: This function is not a strict type-checking function. It will return false if value is a resource variable that has been closed.

Syntax

is_resource(variable)

Parameters

variable Required. Specify the variable being evaluated.

Return Value

Returns true if variable is a resource, false otherwise.

Example:

The example below shows the usage of is_resource() function. Lets assume that we have a file called test.txt in the current working directory.

<?php
$file = fopen("test.txt","r");

if (is_resource($file)) {
  echo "File is opened successfully.";
} else {
  echo "Error in opening the file.";
}
?>

The output of the above code will be:

File is opened successfully.

Example:

Consider one more example, where this function is used with a resource variable that has been closed.

<?php
$file = fopen("test.txt","r");
fclose($file);

if (is_resource($file)) {
  echo "File is opened successfully.";
} else {
  echo "Error in opening the file.";
}
?>

The output of the above code will be:

Error in opening the file.

❮ PHP Variable Handling Reference