Open In App

PHP get_resource_id() Function

The get_resource_id() function is an inbuilt function in PHP that returns the integer id given resource. This function provides a safe way of generating integers for a resource.

Syntax:



get_resource_id($resource) ;

Parameters: This function accepts one parameter that are described below:

Return Value: This function returns the int identifier of the given resource.



Example 1: In the below code example, we will print the id of the given resource.




<?php
  
$res = tmpfile() ;
  
// Print resource id of tmpfile
echo get_resource_id($res);
?>

Output:

4

Example 2: In the below code example, we are opening a text file and then returning his resource id. It may be resource id will be different according to your computer.




<?php
$res = fopen("text.txt", "rb");
echo get_resource_id($res);
?>

Output:

5

Reference: https://www.php.net/manual/en/function.get-resource-id.php

Article Tags :