W3docs

is_resource()

The is_resource() function is a built-in function in PHP that checks whether a variable is a resource or not. A resource is a special variable that holds a

Introduction

is_resource() is a built-in PHP function that checks whether a variable holds a resource. A resource is a special PHP type that acts as a handle to something living outside PHP itself — an open file, a database connection, a network socket, or an image being built in memory. You can't inspect a resource's value directly the way you can a string or an array; you can only pass it to the functions that know how to use it (fread(), fclose(), and so on).

is_resource() returns true when the variable is an open resource and false for every other type, including a resource that has already been closed. This page covers the syntax, runtime behaviour, the important PHP 8 change you need to know about, and how it relates to the other type-checking functions.

Syntax

is_resource(mixed $value): bool

It takes a single argument — the variable you want to test — and returns a boolean. It never throws and never modifies its argument, so it is safe to call on anything.

Example Usage

A file handle returned by fopen() is the most common resource you will meet:

<?php
$handle = fopen("php://temp", "r"); // a real, open resource
$text   = "hello";                  // a plain string

var_dump(is_resource($handle)); // bool(true)
var_dump(is_resource($text));   // bool(false)

fclose($handle);
?>

Use var_dump() rather than echo here: a true echoes as 1, but a false echoes as an empty string, which is easy to misread. var_dump() prints the type and value unambiguously.

A closed resource is no longer a resource

This is the gotcha that trips people up. Once you close a handle, is_resource() returns false for it — it does not stay true. That makes the function a convenient guard against using a handle twice:

<?php
$handle = fopen("php://temp", "w");
var_dump(is_resource($handle)); // bool(true)

fclose($handle);
var_dump(is_resource($handle)); // bool(false) — already closed
?>

Guarding with is_resource() before a fwrite() or a second fclose() prevents the "supplied resource is not a valid stream resource" warnings.

The PHP 8 change: many resources are now objects

In PHP 8.0, a large number of built-in extensions were migrated from resources to opaque objects. Curl handles, GD images, and several other former-resources are now objects, so is_resource() returns false for them even though they conceptually behave the same way:

<?php
$ch = curl_init();          // PHP 7: a resource — PHP 8: a CurlHandle object
var_dump(is_resource($ch)); // bool(false) on PHP 8+
var_dump(is_object($ch));   // bool(true)  on PHP 8+
?>

If you maintain code that runs on both PHP 7 and PHP 8, do not assume a curl or GD handle is a resource. Check for the specific type you expect, or accept both with is_resource($x) || is_object($x).

Finding out which kind of resource you have

When is_resource() is true, get_resource_type() tells you what flavour it is — "stream" for files, "curl" on PHP 7, "gd" for images, and so on:

<?php
$handle = fopen("php://temp", "r");
echo get_resource_type($handle), "\n"; // stream
fclose($handle);
?>

When would I use it?

  • Defensive guards. Before calling fread(), fwrite(), or fclose() on a value you didn't create yourself, confirm it is still an open resource.
  • Functions that accept "a handle or a path". A helper might take either a filename string or an already-open handle; is_resource() lets you branch on which one you got.
  • Cleanup logic. In a finally block or destructor, if (is_resource($h)) fclose($h); avoids closing a handle twice.

is_resource() is one of PHP's family of type-checking predicates. For other types, reach for is_object(), is_array(), or is_string(). If you want the type name as a string instead of a boolean test, use gettype(), which returns "resource" (or "resource (closed)") for handles.

Conclusion

is_resource() confirms that a variable is an open handle to an external resource, returning false for closed handles and for every other type. The one thing to remember in modern PHP is that since PHP 8 many former resources — curl and GD handles among them — are objects, so test with is_object() (or check both) when your code targets PHP 8+.

Practice

Practice
What is the functionality of the is_resource() function in PHP?
What is the functionality of the is_resource() function in PHP?
Was this page helpful?