PHP get_resource_id() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, get_resource_id() returns the unique integer ID of a resource, such as an open file handle. Each resource PHP creates receives its own number.

Available since PHP 8.0. Before PHP 8.0, people wrote (int) $resource to get the same number. get_resource_id() makes that intent clear and is type safe. IDs are handy for logging and debugging, but the exact numbers depend on what your script has already opened.

Syntax

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

PHP
$id = get_resource_id($resource);
CallWhat it does
get_resource_id($handle)Returns the integer ID of the open file handle.
get_resource_id($a) === get_resource_id($b)Checks whether two variables point at the same resource.
get_resource_id("text")Throws a TypeError, because a string is not a resource.

A resource is a special PHP value that points to something outside PHP, such as a file or a stream. The ID is a positive integer. Do not hard-code its value, because it depends on what else your script has opened.

Library declarations
PHP
get_resource_id(resource $resource): int

Parameters

ParameterDescription
resourceRequired. The resource to read the ID from, for example the handle returned by fopen(). A closed resource still has an ID.

Return Value

Return ValueDescription
intThe unique integer ID of the resource.

PHP Examples

Example 1 — Reading a Resource ID

Open a small in-memory stream and ask for its ID. The checks print the same output on every machine.

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

var_dump(is_int($id));
var_dump($id > 0);
var_dump($id === (int) $handle);

fclose($handle);

How It Works

  1. fopen('php://memory', 'r') opens a stream that lives only in memory, so no real file is needed.
  2. get_resource_id() returns the ID as an integer, which is_int() confirms.
  3. IDs start at 1, so the value is greater than 0.
  4. Casting the handle with (int) gives the same number, which is the older way of doing this.

Example 2 — Different Resources, Different IDs

Each new resource gets its own ID, but a copy of a variable points at the same resource.

PHP
<?php
$a = fopen('php://memory', 'r');
$b = fopen('php://memory', 'r');
$copy = $a;

var_dump(get_resource_id($a) !== get_resource_id($b));
var_dump(get_resource_id($b) > get_resource_id($a));
var_dump(get_resource_id($copy) === get_resource_id($a));

fclose($a);
fclose($b);

How It Works

  1. Two separate fopen() calls create two different resources.
  2. Their IDs are different, so comparing them with !== gives true.
  3. $b was opened after $a, so its ID is larger.
  4. $copy = $a does not create a new resource, so $copy and $a have the same ID.

Example 3 — Closed Resources and Wrong Types

A closed resource keeps its ID, but a value that is not a resource is rejected.

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

echo gettype($handle), PHP_EOL;
var_dump(get_resource_id($handle) === $id);

try {
    get_resource_id("abc");
} catch (TypeError $e) {
    echo "TypeError caught", PHP_EOL;
}

How It Works

  1. The handle is closed with fclose(). The variable still exists, but PHP now reports it as a closed resource.
  2. gettype() prints resource (closed) to describe that state.
  3. The ID did not change when the resource was closed, so the comparison is true.
  4. A string is not a resource, so get_resource_id("abc") throws a TypeError, which the catch block handles.

Frequently Asked Questions

It returns the unique integer ID that PHP gave to a resource, such as a file handle returned by fopen().
PHP 8.0. In older versions you could write (int) $resource to get the same number.
No. The ID depends on how many resources the script has already created, including streams PHP opens for itself. Use the ID for debugging only and never hard-code its value.
PHP 8 throws a TypeError, because the argument must be of type resource. Check with is_resource() first if you are not sure what you have.
Yes. A closed resource is still a resource value, so it keeps its ID. Use get_resource_type() or gettype() to learn that it is closed.

Next: get_resource_type()

get_resource_type() returns a string that names the kind of a resource. For a file or stre.

get_resource_type() 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