is_resource

(PHP 4, PHP 5)

is_resource --  检测变量是否为资源类型

描述

bool is_resource ( mixed var )

如果给出的参数 varresource 类型,is_resource() 返回 TRUE,否则返回 FALSE

查看 resource 类型文档获取更多的信息。


add a note add a note User Contributed Notes
tacomage at NOSPAM dot devilishly-deviant dot net
07-Aug-2004 09: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.