简介:本次介绍的是PHP手册中与变量相关的一些系统函数,下面天涯把最常用的进行详细说明。
empty — 检查一个变量是否为空
“”、0、”0″、NULL、FALSE、array()、var $var; 以及没有任何属性的对象都将被认为是空的【天涯注:这个貌似有问题?】
isset — 检测变量是否设置
is_null — 检测变量是否为 NULL
下面是本地环境测试结果:Apache2.2/MySQL5.5/php-5.2.17
<?php
//天涯PHP博客 http://blog.phpha.com
error_reporting(0);
class phpha{}
$var1;
$var2 = '';
$var3 = NULL;
$var4 = array();
$var5 = new phpha;
echo empty($var1) ? 'empty | ' : 'not empty | ';
echo is_null($var1) ? 'is_null | ' : 'not is_null | ';
echo isset($var1) ? 'isset' : 'not isset ';
echo '<br />';
echo empty($var2) ? 'empty | ' : 'not empty | ';
echo is_null($var2) ? 'is_null | ' : 'not is_null | ';
echo isset($var2) ? 'isset' : 'not isset '; 来源gao.dai.ma.com搞@代*码网
echo '<br />';
echo empty($var3) ? 'empty | ' : 'not empty | ';
echo is_null($var3) ? 'is_null | ' : 'not is_null | ';
echo isset($var3) ? 'isset' : 'not isset ';
echo '<br />';
echo empty($var4) ? 'empty | ' : 'not empty | ';
echo is_null($var4) ? 'is_null | ' : 'not is_null | ';
echo isset($var4) ? 'isset' : 'not isset ';
echo '<br />';
echo empty($var5) ? 'empty | ' : 'not empty | ';
echo is_null($var5) ? 'is_null | ' : 'not is_null | ';
echo isset($var5) ? 'isset' : 'not isset ';
?>
//天涯PHP博客 http://blog.phpha.com
输出如下:
empty | is_null | not isset
empty | not is_null | isset
empty | is_null | not isset
empty | not is_null | isset
not empty | not is_null | isset
get_resource_type — 返回资源(resource)类型
<?php
$c = mysql_connect();
echo get_resource_type($c)."\n";
// 打印:mysql link
$fp = fopen("foo","w");
echo get_resource_type($fp)."\n";
// 打印:file
$doc = new_xmldoc("1.0");
echo get_resource_type($doc->doc)."\n";
// 打印:domxml document
?>