PHP: Hypertext Preprocessor (original) (raw)
is_resource
(PHP 4, PHP 5, PHP 7, PHP 8)
is_resource — Finds whether a variable is a resource
Description
Parameters
value
The variable being evaluated.
Examples
Example #1 is_resource() example
`<?php
$handle
= fopen("php://stdout", "w");
if (is_resource($handle)) {
echo '$handle is a resource';
}?>`
The above example will output:
Notes
Note:
is_resource() is not a strict type-checking method: it will return
[false](reserved.constants.php#constant.false)
ifvalue
is a resource variable that has been closed.
Found A Problem?
btleffler [AT] gmail [DOT] com ¶
13 years ago
`I was recently trying to loop through some objects and convert them to arrays so that I could encode them to json strings.
I was running into issues when an element of one of my objects was a SoapClient. As it turns out, json_encode() doesn't like any resources to be passed to it. My simple fix was to use is_resource() to determine whether or not the variable I was looking at was a resource.
I quickly realized that is_resource() returns false for two out of the 3 resources that are typically in a SoapClient object. If the resource type is 'Unknown' according to var_dump() and get_resource_type(), is_resource() doesn't think that the variable is a resource!
My work around for this was to use get_resource_type() instead of is_resource(), but that function throws an error if the variable you're checking isn't a resource.
So how are you supposed to know when a variable is a resource if is_resource() is unreliable, and get_resource_type() gives errors if you don't pass it a resource?
I ended up doing something like this:
The @ operator suppresses the errors thrown by get_resource_type() so it returns null if $possibleResource isn't a resource.
I spent way too long trying to figure this stuff out, so I hope this comment helps someone out if they run into the same problem I did.
`
7 years ago
`` Note that is_resource() is unreliable. It considers closed resources as false:
That's the reason why some other people here have been confused and devised some complex (bad) "solutions" to detect resources...
There's a much better solution... In fact, I just showed it above, but here it is again with a more complete example:
So how do you check if something is a resource?
Like this!
isResource=isresource(isResource = is_resource(isResource=isresource(a) || ($a !== null && !is_scalar($a) && !is_array($a) && !is_object($a)); var_dump($isResource); //bool(true)fclose($a);var_dump(is_resource($a)); //bool(false)$isResource = is_resource($a) || ($a !== null && !is_scalar($a) && !is_array($a) && !is_object($a)); var_dump($isResource); //bool(true)?>How it works:
- An active resource is a resource, so check that first for efficiency.
- Then branch to check what the variable is NOT:
- A resource is never NULL. (We do that check via
!== null
for efficiency). - A resource is never Scalar (int, float, string, bool).
- A resource is never an array.
- A resource is never an object.
- Only one variable type remains if all of the above checks succeeded: IF it's NOT any of the above, then it's a closed resource!
Just surfed by and saw the bad and hacky methods other people had left, and wanted to help out with this proper technique. Good luck, everyone!
PS: The core problem is that is_resource() does a "loose" check for "living resource". I wish that it had a $strict parameter for "any resource" instead of these user-workarounds being necessary.
``
11 years ago
`Try this to know behavior:
', $resource ? 'TRUE' : 'FALSE', PHP_EOL, 'get_resource_type($resource) => ', get_resource_type($resource) ?: 'FALSE', PHP_EOL, 'is_resource($resource) => ', is_resource($resource) ? 'TRUE' : 'FALSE', PHP_EOL, PHP_EOL ; }$resource = tmpfile(); resource_test($resource, 'Check Valid Resource');fclose($resource); resource_test($resource, 'Check Released Resource');$resource = null; resource_test($resource, 'Check NULL'); ?>It will be shown as...
[Check Valid Resource]
(bool)$resource => TRUE
get_resource_type($resource) => stream
is_resource($resource) => TRUE
[Check Released Resource]
(bool)$resource => TRUE
get_resource_type($resource) => Unknown
is_resource($resource) => FALSE
[Check NULL]
(bool)$resource => FALSE
get_resource_type($resource) => FALSE
Warning: get_resource_type() expects parameter 1 to be resource, null given in ... on line 10
is_resource($resource) => FALSE
`