PHP Variable Handling get_resource_id() Function



The PHP Variable Handling get_resource_id() function is used to get a resource's integer ID. In PHP, resources are specific data types such as database connections and file handles. This function returns the unique ID linked to a resource. It is a secure and simple method to get the ID without changing the resource.

You give the resource to the function, and it returns an integer as the ID. This feature is useful when you want to locate or interact with specific resources. It is the same as converting a resource to an integer, but in a secure way. This function improves resource management in your PHP programs.

Syntax

Below is the syntax of the PHP Variable Handling get_resource_id() function −

int get_resource_id ( resource $resource )

Parameters

This function accepts $resource parameter which is resource handle that you want to evaluate. It can be a database connection, a file or any other resource type.

Return Value

The get_resource_id() function returns an integer that uniquely identifies the given resource. This integer is the ID assigned to the resource by PHP.

PHP Version

Introduced in core PHP 8.

Example 1

This program opens a file with fopen() and returns its resource ID using the PHP Variable Handling get_resource_id() function. It shows the function's basic functioning with a simple file resource.

<?php
   // Open a file to create a resource
   $file = fopen("/PHP/PhpProjects/myfile.txt", "w");

   // Get the resource ID
   $resourceId = get_resource_id($file);
   echo "Resource ID for the file: " . $resourceId;

   // Close the file
   fclose($file);
?>

Output

Here is the outcome of the following code −

Resource ID for the file: 5

Example 2

This program uses fsockopen() to create a socket connection and returns the resource ID using the get_resource_id() function. It shows the function's response to a network resource.

<?php
   // Open a socket connection
   $socket = fsockopen("www.tutorialspoint.com", 80);

   // Check if the socket connection is successful
   if ($socket) {
      // Get the resource ID
      $resourceId = get_resource_id($socket);
      echo "Resource ID for the socket: " . $resourceId;

      // Close the socket
      fclose($socket);
   } else {
      echo "Socket connection failed!";
   }
?> 

Output

This will generate the below output −

Resource ID for the socket: 4

Example 3

Now the below code shows you how to handle many resources in an array, get resource IDs and display them with the help of get_resource_id() function. It allows you to manage effectively multiple resources.

<?php
   // Create multiple resources
   $file = fopen("myfile.txt", "w");
   $conn = mysqli_connect("localhost", "root", "", "test_db");
   $socket = fsockopen("www.tutorialspoint.com", 80);

   // Store resources in an array
   $resources = [$file, $conn, $socket];

   // Display resource IDs
   foreach ($resources as $key => $resource) {
      echo "Resource " . ($key + 1) . " ID: " . get_resource_id($resource) . "\n";
   }

   // Close all resources
   fclose($file);
   mysqli_close($conn);
   fclose($socket);
?> 

Output

This will create the below output −

Resource 1 Type: stream
Resource 2 Type: mysql link
Resource 3 Type: stream
php_function_reference.htm
Advertisements