天天看点

php基础之isset unset和empty

直接上代码,如下:

/**
 * isset:
 *  判断一个变量是否存在,以及是否赋值
 * unset:
 *  释放一个变量,断开引用,会被垃圾回收器回收,该变量再次调用isset时,返回false
 * empty:
 *  判断一个变量是否为空,以及是否赋值
 *      0 "0" null false "" array()  都是empty
 */
if (isset($s1)) {
    echo "isset<br>";
} else {
    echo "not set<br>";// will print
}

if (empty($s1)) {
    echo "empty<br>";// will print
} else {
    echo "not empty<br>";
}

if (empty("")) {
    echo "empty<br>";// will print
} else {
    echo "not empty<br>";
}

if (empty("0")) {
    echo "empty<br>";// will print
} else {
    echo "not empty<br>";
}

if (empty(array())) {
    echo "empty<br>";// will print
} else {
    echo "not empty<br>";
}

$s2 = 10;
if (isset($s2)) {
    echo "is set<br>";// will print
} else {
    echo "not set<br>";
}

unset($s2);//释放掉
if (isset($s2)) {
    echo "is set<br>";
} else {
    echo "not set<br>";// will print
}