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