检查if语句中是否存在对象

时间:2018-11-26 10:17:23

标签: php laravel

如何检查OR语句中是否存在属性,然后检查IF语句中是否设置了属性?

    @if($counsellingSession->has_session_begun == 1 || !empty($thread->close_session == 1))
         disabled
    @elseif($loop->index > 7 && $data->payment_type == 'free')
         disabled
    @endif

3 个答案:

答案 0 :(得分:3)

尝试property_exist php函数(如果存在)将为您提供true其他明智的false

property_exist($thread,'close_session')

了解property_exist

http://php.net/manual/en/function.property-exists.php

答案 1 :(得分:1)

也许使用isset?它将检查对象是否存在或值为空

@if(isset($thread->close_session) && $thread->close_session == 1)
@endif

答案 2 :(得分:1)

您可以使用property_exists吗? 例如:

@if((property_exists($counsellingSession,"has_session_begun") && $counsellingSession->has_session_begun == 1) || (property_exists($thread,"close_session") && $thread->close_session == 1 ) )
         disabled
    @elseif($loop->index > 7 && $data->payment_type == 'free')
         disabled
    @endif