Are PHP resource IDs unique?

Are PHP resource identifiers unique to each PHP instance? Listing all kinds of resources per line returns "Resource Identifier #X" (where X is some decimal number)? Is there a function that returns the resource identifier (I know about get_resource_type() to get the type of the resource, but did not find anything like get_resouce_id() ), or should I do this?

 function get_resource_id($resource) { return is_resource($resouce) ? substr((string) $resource, 13 /* strlen("Resource id #") */) : NULL; } 
+4
source share
2 answers

Jakub, yes PHP will always provide you with a unique identifier for each resource that is currently in use. When a resource is disabled () or free, for example mysql_free_result . This identifier is then available again, and PHP can reuse these resources.

Be careful if you "cache" or save the resource identifier in the session and reuse them, because PHP can free the resource and then recreate a new resource with this identifier, and your old resource identifier may point to something new.

So, you have it, the resources are unique, but not forever!

We hope this helps demystify the resource types in PHP for you.

+4
source

Easy as he stitches

 intval($resource) 
+6
source

All Articles