Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Types and Type Handling

PHP resources

Reference for the PHP resource type: file and stream handles, get_resource_type(), is_resource(), closed resources and the move to objects in PHP 8.

A resource is a special value that holds a reference to something outside PHP's normal data: an open file, a stream, a directory handle or a database connection from an older extension. You never create one with a literal. Functions such as fopen(), opendir() and stream_context_create() return them, and other functions accept them as arguments.

Resources are being phased out. Since PHP 8.0, many extensions return objects instead (CurlHandle, GdImage, Socket, XMLWriter and others). Files and streams still use resources.

PHP
$handle = fopen("php://memory", "w+");
var_dump(is_resource($handle));       // bool(true)
echo get_resource_type($handle);      // stream
fclose($handle);

Try it

PHP Opening, inspecting and closing a resource

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Resources are not scalars

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
$resource = fopen($path, $mode);          // creates
is_resource($value): bool                  // true for open resources
get_resource_type($resource): string      // "stream", "stream-context", ...
get_resource_id($resource): int           // the number (PHP 8.0)
Function Purpose
is_resource() true only for an open resource; a closed one gives false.
get_resource_type() The kind of resource; "Unknown" for a closed one.
get_resource_id() Numeric id, the same as (int) $resource.
gettype() "resource", or "resource (closed)" after closing.
get_debug_type() "resource (stream)", or "resource (closed)".

Usage notes

  • Always close what you open. PHP closes resources when the script ends and when the last reference disappears, but explicit fclose() frees file locks and handles early.
  • Closed resources stay resource-like values, so test with is_resource() rather than !== false. Passing a closed stream to fwrite() throws a TypeError in PHP 8.
  • Check return values. fopen() returns false on failure (with a warning), not a resource.
  • Resources cannot be serialized or encoded to JSON (JSON encoding fails with an error, serialize() stores them as integers 0).
  • Casting to int gives the id; casting to bool is always true.
  • Do not compare ids across scripts. The number is only unique within one request.
  • The declaration resource does not exist. You cannot write function f(resource $r). Omit the type or check is_resource() inside the function.
  • Prefer objects for new code that wraps streams, for example SplFileObject.