32 of 410 menu

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

is_resource(mixed $var): bool

Example

Let's check if a variable is a resource:

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

Code execution result:

true

Example

Let's check a regular variable:

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

Code execution result:

false

Example

Let's check a closed resource:

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

Code execution result:

false

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
byenru