laravel中empty(),is_null() 以及isEmpty()


PHP中

empty()

empty() 函數用於檢查一個變量是否為空。

if(empty($result->order)){
    //操作
}

is_null()

is_null() 函數用於檢測變量是否為 NULL。

     $code = Input::get('code');
       if (!is_null($code)) {
            return Response::json(['status' => 1]);
        } else {
            return Response::json(['status' => 0]);
        }
    //first也是
     $select = Location::select('…')->where(…)->first()
        if(is_null($select)){
            //操作
        }else{
            //操作
        }

laravel 中

isEmpty()

在使用 Laravel Eloquent 模型時,我們要判斷取出的結果集是否為空時,以上兩種方法無效,但是laravel提供了isEmpty()給我們

$select = Location::select('…')->where(…)->get()
//判斷查詢結果是否為空
if(isEmpty($select)){
//或if(empty($select)){
//或if($select->isEmpty)){
    //操作
}else{
    //操作
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM