search for in the  
<is_realis_scalar>
Last updated: Thu, 19 May 2005

is_resource

(PHP 4, PHP 5)

is_resource --  Finds whether a variable is a resource

Description

bool is_resource ( mixed var )

Finds whether the given variable is a resource.

Parameters

var

The variable being evaluated.

Return Values

Returns TRUE if var is a resource, FALSE otherwise.

Examples

Example 1. using is_resource()

<?php

$db_link
= @mysql_connect('localhost', 'mysql_user', 'mysql_pass');
if (!
is_resource($db_link)) {
   die(
'Can\'t connect : ' . mysql_error());
}

?>

See Also

The resource-type documentation
get_resource_type()



User Contributed Notes
is_resource
tacomage at NOSPAM dot devilishly-deviant dot net
06-Aug-2004 08:10
Note that the use of is_resource isn't necessary in the example.  mysql_connect (along with any other function that would return a resouce, I imagine) returns false on failure, so the same results could be obtained with:
<?php

$db_link
= @mysql_connect('localhost', 'mysql_user', 'mysql_pass');
if (!
$db_link) {
   die(
'Can\'t connect : ' . mysql_error());
}

?>

Or even:
<?php
  $db_link
= @mysql_connect('localhost', 'mysql_user', 'mysql_pass')
  or die(
'Can\'t connect : ' . mysql_error());
}
?>

You'd be more likely to use is_resource AFTER the initial conection, to make sure the variable you intend to use as a resource is, in fact, a connection resource.  You might also use is_resource as a sanity-check prior to serializing an object, since resource variables can't be serialized.
ccjeagle at ms6 dot hinet dot net
09-Aug-2000 12:15
// A simple example:
$fp = fopen("my.txt","r") ;
echo gettype($fp) ;
print is_resource($fp) ? "TRUE" : "FALSE" ;
fclose($fp) ;

// Another example
$fp = fopen("my.txt","r") ;
fclose($fp) ;
echo gettype($fp) ;
print is_resource($fp) ? "TRUE" : "FALSE" ;

Both return "resource" and "TRUE".

<is_realis_scalar>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT