PHP is_resource() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, is_resource() checks whether a value is an open resource. A resource is a special handle that PHP gives you for things such as open files and streams.

When you open a file with fopen(), PHP returns a resource that you pass to functions like fwrite() and fclose(). After you close the file, the variable still exists, but it is no longer a valid resource, and is_resource() returns false.

Syntax

Call is_resource() with the values you need. Start with this line.

PHP
$ok = is_resource($handle);
CallWhat it does
is_resource(fopen('php://memory', 'r'))Returns true because fopen() gives back an open resource.
is_resource('php://memory')Returns false because a string is only text, not a resource.
is_resource($closedHandle)Returns false after fclose() has closed the handle.

A closed resource is shown by gettype() as 'resource (closed)', and is_resource() returns false for it. Many extensions in PHP 8 now return objects instead of resources, so check their own docs.

Library declarations
PHP
is_resource(mixed $value): bool

Parameters

ParameterDescription
valueRequired. The value you want to check. It can be of any type.

Return Value

Return ValueDescription
boolReturns true if the value is an open resource, and false otherwise, including for closed resources.

PHP Examples

Example 1 — Checking a File Handle

Open an in-memory stream with fopen() and compare it with a string and null.

PHP
<?php
$handle = fopen('php://memory', 'r+');

var_dump(is_resource($handle));
var_dump(is_resource('php://memory'));
var_dump(is_resource(null));

fclose($handle);

How It Works

  1. fopen() opens a stream. The php://memory stream lives in memory, so no real file is needed.
  2. The handle is an open resource. is_resource() returns true for it.
  3. The string and null are not resources. They return false even though the string names a stream.
  4. fclose() frees the handle. It is good practice to close every handle you open.

Example 2 — Open Versus Closed Resources

See how gettype() and is_resource() change after a handle is closed.

PHP
<?php
$handle = fopen('php://memory', 'r+');
echo gettype($handle), PHP_EOL;

fclose($handle);

var_dump(is_resource($handle));
echo gettype($handle), PHP_EOL;

How It Works

  1. While open, gettype() says resource. The handle is valid and can be used.
  2. fclose() closes the stream. The variable $handle still holds the old value.
  3. is_resource() returns false. A closed resource is no longer a valid resource.
  4. gettype() now says resource (closed). This is how PHP marks a dead handle.

Example 3 — Closing a Handle Safely

Write to a memory stream, read the text back, and close the handle only if it is still a resource.

PHP
<?php
$handle = fopen('php://memory', 'w+');
fwrite($handle, 'Hello');
rewind($handle);
echo fread($handle, 5), PHP_EOL;

if (is_resource($handle)) {
    fclose($handle);
    echo 'Closed', PHP_EOL;
}

How It Works

  1. The stream is opened for reading and writing. The w+ mode allows both.
  2. fwrite() puts the text in and rewind() moves back to the start. Then fread() reads the 5 characters.
  3. is_resource() guards the cleanup. It only returns true when the handle is still open.
  4. fclose() runs and the script prints Closed. This avoids errors from closing a handle twice.

Frequently Asked Questions

It checks whether a value is an open resource, such as a file handle or stream returned by fopen(). It returns true for open resources and false for everything else.
Closing a handle makes it invalid. PHP changes its type to 'resource (closed)', and is_resource() returns false for closed resources.
A resource is a special variable type that holds a reference to an outside thing, like an open file, a stream or a database connection. You create one with functions such as fopen().
Since PHP 8.0, curl_init() returns a CurlHandle object instead of a resource. Many other extensions made the same change, so use is_object() or instanceof for them.
Use get_resource_type() to get a name such as 'stream'. You can also use get_resource_id() to get its numeric id.

Next: is_scalar()

is_scalar() checks whether a value is a scalar. The scalar types are int, float, string an.

is_scalar() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful