iT邦幫忙

1

廢肥菜比八練習isset empty

php

根據https://newaurora.pixnet.net/blog/post/171735396-php-isset%28%29%E5%92%8Cempty%28%29%E7%9A%84%E5%8D%80%E5%88%A5
所描述的isset empty 來做練習

$b = NUll;

$c = 0;

$f = "0";

$d = 1;

$e = ""; 

$g = "Abc";

//下面用isset() empty()函數來測試
//isset()函數是用來判斷變數是不是有存在,如果有就回傳 1(true),如果沒有就回傳空值
//empty()函數用來判斷"值"是不是空的,如果沒有就回傳 1(true),如果有"值"就不回傳

// a不存在
// isset a=
// empty a=TRUE(1)
echo "isset a=" . isset($a) . "<br>"; //$a是存在嗎? => 沒有就回傳空值
echo "empty a=" . empty($a) . "<br>"; //$a是空的嗎? => 是空的 => empty a=1

// b=NULL
// isset b=
// empty b=TRUE(1)
echo "isset b=" . isset($b) . "<br>"; //$b是存在嗎? => 沒有就回傳空值
echo "empty b=" . empty($b) . "<br>"; //$b是空的嗎? => 是空的 => empty b=1

// c=0 (int)
// isset c=TRUE(1)
// empty c=TRUE(1)
// 0(int) 被empty當成沒有了?!!
echo "isset c=" . isset($c) . "<br>"; //$c是存在嗎? => 有存在回傳1
echo "empty c=" . empty($c) . "<br>"; //$c是空的嗎? => 0(int)是空的 => empty c=1
echo "-----------------------------<br>";

// f="0" (string)
// isset f=TRUE(1)
// empty f=TRUE(1)
// "0"(string) 被empty當成沒有了?!!
echo "isset f=" . isset($f) . "<br>"; //$f是存在嗎? => 有存在回傳1
echo "empty f=" . empty($f) . "<br>"; //$f是空的嗎? => 0(string)是空的 => empty f=1

// d=1 (int)
// isset d=TRUE(1)
// empty d=
echo "isset d=" . isset($d) . "<br>"; //$d是存在嗎? => 有存在回傳1
echo "empty d=" . empty($d) . "<br>"; //$d是空的嗎? => 1(int)有值 => 不回傳

// e="" (string)
// isset e=TRUE(1)
// empty e=TRUE(1)
echo "isset e=" . isset($e) . "<br>"; //$e是存在的嗎 => 有存在回傳1
echo "empty e=" . empty($e) . "<br>"; //$e是空的嗎? => ""(string)是空的 => empty e=1

// g="Abc" (string)
// isset g=TRUE(1)
// empty g=
echo "isset g=" . isset($g) . "<br>"; //$g是存在嗎? => 有存在回傳1
echo "empty g=" . empty($g) . "<br>"; //$g是空的嗎? => "Abc"(string)有值 => 不回傳

b=NULL
isset b=
在PHP手冊中就有說到
https://www.php.net/isset
isset() will return FALSE if testing a variable that has been set to NULL
stackoverflow 中的問題
https://stackoverflow.com/questions/7191626/isset-and-empty-what-to-use

官方出的比較Table表
https://www.php.net/manual/en/types.comparisons.php

比較特別的狀況_浮點數 與 字串"浮點數"
拿在上述中的 c=0(int) 與 f="0"(string)中
c = 0 //isset c=TRUE(1) empty c=TRUE(1)
f="0" //isset f=TRUE(1) empty f=TRUE(1)

$a = 0;
//照裡來說
// isset a=TRUE(1)
// empty a=TRUE(1)
//事實是
// isset a=1
// empty a=1
echo "isset a=" . isset($a) . "<br>";
echo "empty a=" . empty($a) . "<br>";


$b = 0.00;
//照裡來說
// isset b=TRUE(1)
// empty b=TRUE(1)
//事實是
// isset b=1
// empty b=1
echo "isset b=" . isset($b) . "<br>";
echo "empty b=" . empty($b) . "<br>";

$c = "0.0" . "<br>";
//照裡來說
// isset c=TRUE(1)
// empty c=TRUE(1)
//事實是
// isset c=1
//  ** empty c=
echo "isset c=" . isset($c) . "<br>";
echo "empty c=" . empty($c) . "<br>";  // 此判定有值 => 不回傳

$c = "0.0"
empty "0.0" = 判定有值... => 不回傳
就某些狀況真的容易搞錯就是了

<---2020-05-26更新--->

今天同事問了個問題,結果自己也搞混了empty()
假如陣列裡的第一筆是空值,那陣列是否為空;

個人一直是以 是否為空值 來做empty的邏輯判斷,但程式再跑時
array就是一筆一筆去跑的,所以empty(array(第一筆資料為0,或是空值時)) empty會過嗎?
的問題就出來了

假如資料長這樣

    $a = array(
        array('0', ''),
        array('name', 'jamce'),
        array('ID', 123),
     );
    
    print_r($a);
    
    if (empty($a)){
        echo 'a is empty';
    } else {
        echo 'a is not empty';
    }

可以發現print出來是這樣

Array
(
    [0] => Array
        (
            [0] => 0
            [1] => 
        )

    [1] => Array
        (
            [0] => name
            [1] => jamce
        )

    [2] => Array
        (
            [0] => ID
            [1] => 123
        )

)
a is not empty

那改成這樣呢?

    $a = array(
      array('', ''),            //第一筆的資料為空
      array('name', 'jamce'),
      array('ID', 123),
    );

print出來是這樣,第一筆 的第一個值為空

Array
(
    [0] => Array
        (
            [0] => 
            [1] => 
        )

    [1] => Array
        (
            [0] => name
            [1] => jamce
        )

    [2] => Array
        (
            [0] => ID
            [1] => 123
        )

)                            // a is not empty

喔喔!果然不是空值

    $a = array(
      array('', ''),        // 改成全空值
      array('', ''),
      array('', ''),
    );
Array
(
    [0] => Array
        (
            [0] =>          // 還有key
            [1] => 
        )

    [1] => Array
        (
            [0] => 
            [1] => 
        )

    [2] => Array
        (
            [0] => 
            [1] => 
        )

)                       // a is not empty

改為0

    $a = array(         // a is not empty
    array(0, 0)
    );

Array裡只剩下空 array()! 跟Array裡只剩下''

    $a = array(
      array()
    );                  // a is not empty
    
    $a = array(         // Array([0] => ) a is not empty  
      ''
    );

結論,empty是不是空的判斷 在陣列中,與陣列帶的值無關,只要裡面有東西empty就不成立


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言