The is_resource Function

The is_resource function checks if the passed variable is a resource. A resource is a special data type in PHP that references external resources, such as files, database connections, etc. The function returns true if the variable is a resource, and false otherwise.

Syntax

phpis_resource(mixed $var): bool

Example

Let's check if a variable is a resource:

php<?php
  $file = fopen('test.txt', 'r');
  $res = is_resource($file);
  var_dump($res);
  fclose($file);
?>

Code execution result:

phptrue

Example

Let's check a regular variable:

php<?php
  $var = 'hello';
  $res = is_resource($var);
  var_dump($res);
?>

Code execution result:

phpfalse

Example

Let's check a closed resource:

php<?php
  $file = fopen('test.txt', 'r');
  fclose($file);
  $res = is_resource($file);
  var_dump($res);
?>

Code execution result:

phpfalse

See Also

  • the is_array function,
    which checks if a variable is an array
  • the is_int function,
    which checks if a variable is an integer
  • the is_string function,
    which checks if a variable is a string