What is the get_resource_id() method in PHP?

The get_resource_id method can be used to get the resource ID of the specific resource. This method is introduced in PHP 8.

In PHP, the resource denotes a reference to an external resource. For example, a resource variable can point to a file. You must assign a specific integer ID – called the resource ID – to each resource PHP.

Syntax

get_resource_id(resource $resource): int

This method returns an int value.

Example

<?php
$res = tmpfile();
echo "The resource id of the temporary file is :";
echo get_resource_id($res) + "\n";
?>

Output

The resource id of the temporary file is : 543

In the code above,

  • We have created a temporary file using the tmpfile method.

  • We have used the get_resource_id method to get the resource identifier number of the resource.

Free Resources