• PHP Video Tutorials

PHP - is_resource() Function



Definition and Usage

The function is_resource() checks whether a variable is a resource.

Syntax

bool is_resource ( mixed $value )

Parameters

Sr.No Parameter & Description
1

value

The variable being evaluated.

Return Values

This function returns true if value is a resource, false otherwise. This function is not a strict type-checking method: it will return false if value is a resource variable that has been closed.

Dependencies

PHP 4 and above

Example

Following example demonstrates use of is_resource() function (Here test.txt is a dummy text file placed on the same path as this example file) −

<?php
   $file = fopen("test.txt","w");
   if (is_resource($file)) {
      echo "File is open";
   } else {
      echo "Error open file";
   }
?>

Output

This will produce following result −

File is open

Example

Following example demonstrates use of is_resource() function when resource is closed (Here test.txt is a dummy text file placed on the same path as this example file) −

<?php
   $file = fopen("test.txt","w");
   fclose($file);
   if (is_resource($file)) {
      echo "File is open";
   } else {
      echo "Error open file";
   }
?>

Output

This will produce following result −

Error open file
php_variable_handling_functions.htm
Advertisements