tags:

views:

53

answers:

2

Are PHP resource IDs unique per PHP instance? Does casting all kinds of resources to string returns "Resource id #X" (where X is some decimal number)? Is there a function that returns resource ID (I know about get_resource_type() to get resource typem, but didn't find anything like get_resouce_id()), or does it have to be done like this?

function get_resource_id($resource)
{
    return is_resource($resouce) 
           ? substr((string) $resource, 13 /* strlen("Resource id #") */)
           : NULL;
}
A: 

You can echo resource to get resource ID.

For example: echo mysql_query('select ...'); // will return Resource ID #XX

Māris Kiseļovs
+3  A: 

Jakub, yes PHP will always give you a unique ID for every resource that's currently in use. When a resource is unset() or free'd such as mysql_free_result. Then that ID is available again and PHP could reuse these resources again.

Be wary if you're "caching" or keeping resource ID's in the session and reusing them because PHP may free a resource, then recreate a new resource with that ID and your old resource ID might be pointing to something new.

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

Hope this helps demystify resource types in PHP for you.

Paul Dragoonis
Is it not possible that in certain environments 2 different users could get the _same_ resource ID when connecting to the same (shared) resource? Or is that impossible?
w3d