PHP 5.4.33 Released

is_resource

(PHP 4, PHP 5)

is_resource Prüft, ob eine Variable vom Typ resource ist

Beschreibung

bool is_resource ( mixed $var )

Prüft, ob die gegebene Variable eine Ressource ist.

Parameter-Liste

var

Die zu untersuchende Variable.

Rückgabewerte

Gibt TRUE zurück, wenn var vom Typ resource ist, andernfalls FALSE.

Beispiele

Beispiel #1 is_resource()-Beispiel

<?php

$db_link 
= @mysql_connect('localhost''mysql_user''mysql_pass');
if (!
is_resource($db_link)) {
    die(
'Verbindung konnte nicht hergestellt werden : ' mysql_error());
}

?>

Siehe auch

add a note add a note

User Contributed Notes 2 notes

up
4
btleffler [AT] gmail [DOT] com
3 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:

<?php

function isResource ($possibleResource) { return !is_null(@get_resource_type($possibleResource)); }

?>

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.
up
0
CertaiN
8 months ago
Try this to know behavior:

<?php
function resource_test($resource, $name) {
    echo
       
'[' . $name. ']',
       
PHP_EOL,
       
'(bool)$resource => ',
       
$resource ? 'TRUE' : 'FALSE',
       
PHP_EOL,
       
'get_resource_type($resource) => ',
       
get_resource_type($resource) ?: 'FALSE',
       
PHP_EOL,
       
'is_resoruce($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_resoruce($resource) => TRUE

[Check Released Resource]
(bool)$resource => TRUE
get_resource_type($resource) => Unknown
is_resoruce($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_resoruce($resource) => FALSE
To Top