PHP is_null() isset() empty() 区别

2017-07-14  本文已影响24人  tristax

PHP is_null() isset() empty() 区别

简要阐述基本用法,然后展示相关示例

is_null()

bool is_null ( mixed $var ) 查看所给的变量是否为 NULL

当参数满足以下三种情况时,is_null() 将返回 TRUE

isset()

bool isset ( mixed $var [, mixed $... ] ) 检测变量是否设置,并且不是 NULL

参数满足以下几种情况时,isset() 将返回 FALSE

Note: 如果传入多个参数时,将返回取交集后的结果

empty()

bool empty ( mixed $var ) 检查一个变量是否为空

当 var 存在,并且是一个非空非零时返回 FALSE 否则返回 TRUE, 当参数满足以下情况时,empty() 将返回 TRUE

Note: 在 php 5.5.0 版本后已支持表达式

示例对比

<?php
error_reporting(~E_NOTICE);
$a = 10;
$b = "";
$c = NULL;

echo "\n\n";
echo '$a = 10, $b = "", $c = NULL';

echo "\n\n============ ISSET ===========\n";
function testIsset($a, $b, $c)
{
    echo 'isset($a): ' . (isset($a) ? "Defined" : "Undefined") . "\n";
    echo 'isset($b): ' . (isset($b) ? "Defined" : "Undefined"). "\n";
    echo 'isset($c): ' . (isset($c) ? "Defined" : "Undefined") . "\n";
    unset($b);
    echo 'unset($b)';
    echo "\n";
    echo 'isset($b): ' . (isset($b) ? "Defined" : "Undefined") . "\n";
}
testIsset($a, $b, $c);
echo "============= END ============\n\n";

echo "============ EMPTY ===========\n";
function testEmpty($a, $b, $c)
{
    echo 'empty($a): ' . (empty($a) ? "empty" : "not empty") . "\n";
    echo 'empty($b): ' . (empty($b) ? "empty" : "not empty") . "\n";
    echo 'empty($c): ' . (empty($c) ? "empty" : "not empty") . "\n";
    unset($b);
    echo 'unset($b)';
    echo "\n";
    echo 'empty($b): ' . (empty($b) ? "empty" : "not empty") . "\n";
}
testEmpty($a, $b, $c);
echo "============= END ============\n\n";

echo "============ IS_NULL ===========\n";
function testIsnull($a, $b, $c)
{
    echo 'is_null($a): ' . (is_null($a) ? "null" : "not null") . "\n";
    echo 'is_null($b): ' . (is_null($b) ? "null" : "not null") . "\n";
    echo 'is_null($c): ' . (is_null($c) ? "null" : "not null") . "\n";
    echo 'unset($b)';
    echo "\n";
    echo 'is_null($b): ' . (is_null($b) ? "null" : "not null") . "\n";
}
testIsnull($a, $b, $c);
echo "============== END =============\n\n";
?>

参考原文地址:http://www.111cn.net/phper/php/47918.htm

上一篇 下一篇

猜你喜欢

热点阅读