W3docs

get_resource_type()

The get_resource_type() function is a built-in function in PHP that returns the resource type of a given resource. A resource is a special type of variable that

Introduction

get_resource_type() is a built-in PHP function that returns a string naming the kind of resource a variable holds — for example "stream" for an open file, or "process" for a running external program.

A resource is a special PHP variable that holds a reference to something living outside PHP: an open file, a network socket, an image being built in memory, a child process, and so on. Because the underlying object is opaque, you can't inspect it directly. get_resource_type() gives you a human-readable label for what the resource is, which is handy for logging, debugging, and writing functions that accept several resource kinds but only handle some of them.

This page covers the syntax, the resource type strings you'll actually meet, an important PHP 8 caveat (many former "resources" are now objects), and how to use the function safely.

Syntax

get_resource_type(resource $handle): string

It takes a single parameter:

  • $handle — the resource whose type you want. It must be an active resource value.

It returns a lowercase string identifying the resource type. If you pass anything that is not a resource, PHP 8 throws a TypeError (in PHP 7 it emitted a warning and returned null).

Basic example

The most common resource you'll inspect is a file/stream handle from fopen():

<?php
$file = fopen("php://memory", "w+");

echo get_resource_type($file), PHP_EOL; // stream

fclose($file);
?>

Output:

stream

php://memory is a real, always-available stream, so this example runs anywhere without needing a file on disk.

Common resource type strings

Different functions create different resource types. A few you're likely to encounter:

<?php
$file = fopen("php://memory", "r");
echo get_resource_type($file), PHP_EOL;   // stream

$ctx = stream_context_create();
echo get_resource_type($ctx), PHP_EOL;    // stream-context

$proc = proc_open("echo hi", [], $pipes);
echo get_resource_type($proc), PHP_EOL;   // process
proc_close($proc);
?>

Output:

stream
stream-context
process

The exact strings come from the extension that created the resource, so they are stable names you can compare against ("stream", "curl" on older PHP, "gd" for older image handles, etc.).

Watch out: PHP 8 turned many resources into objects

This is the most important gotcha. Across PHP 8.0, several extensions migrated their old resource types to real objects. cURL handles, MySQLi connections, GD images, and others are no longer resources — so get_resource_type() now throws a TypeError on them:

<?php
$ch = curl_init();           // PHP 8: returns a CurlHandle object, not a resource
echo gettype($ch), PHP_EOL;  // object

// get_resource_type($ch);   // TypeError: must be of type resource, CurlHandle given
?>

Output:

object

So older tutorials that show get_resource_type($curlHandle) returning "curl" are out of date. For these object-based handles, use get_class() / instanceof instead. File streams from fopen() are still genuine resources, which is why they remain the reliable example.

Guarding against non-resources

Because passing a non-resource raises a TypeError in PHP 8, check first with is_resource() when the value might be closed or false:

<?php
$file = fopen("php://memory", "r");
fclose($file);

if (is_resource($file)) {
    echo get_resource_type($file);
} else {
    echo "Not an active resource";
}
?>

Output:

Not an active resource

After fclose(), the variable becomes a closed resource: is_resource() returns false, and gettype() reports "resource (closed)". Guarding like this avoids a fatal TypeError.

When would I use this?

  • Validating input to a function that should only accept a specific resource kind (e.g. reject anything that isn't a "stream").
  • Debugging / logging — recording what a resource actually is when tracking down a bug.
  • Branching logic in libraries that handle several resource types differently.

For a plain "is this a resource at all?" check, prefer is_resource(); reach for get_resource_type() only when you need to distinguish which resource it is.

Conclusion

get_resource_type() returns a string describing what kind of resource a variable holds, which is useful for validation, debugging, and type-specific branching. Remember the PHP 8 shift: many handles that used to be resources (cURL, MySQLi, GD) are now objects and will trigger a TypeError — for those, use get_class() instead. Pair get_resource_type() with is_resource() so you never call it on a closed or invalid value.

Practice

Practice
What does the get_resource_type() function in PHP do?
What does the get_resource_type() function in PHP do?
Was this page helpful?