The is_resource
method can be used to check if a variable is a resource
or not.
is_resource(mixed $value): bool
This method returns true
if the passed value is a resource
. Otherwise, it returns false
.
Refer here for the list of resources available in PHP.
For closed resources, the is_resource
method returns false
.
<?php$test = fopen("foo", "w");printIsResource($test);$test = tmpfile();printIsResource($test);fclose($test);printIsResource($test);$test = false;printIsResource($test);function printIsResource($test){echo "The Value is: ";var_dump($test);echo "Is Resource: ";var_dump(is_resource($test));echo "---------\n\n";}?>
In the code above:
We open a file, foo
,
and call the is_resource
method. For this case, we will get true
as the result.
We use the tmpfile
method to create a temporary file and call the is_resource
method. For this case, we will get true
as the result.
Then, we use fclose
to close the created temporary file and call the is_resource
method again. Now, we will get false
as the result because the is_resource
method returns false
for closed resources.