Level 4
if ($booking->guest && $booking->guest->first_name == 'John') {}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi all!
I've got a simple question regarding PHP and not Laravel only.
In my code I often have statements like if($booking->guest->first_name == 'John') which throws a notice if guest is not defined. Of course if guest is not defined the guest is not John.
What's the best way to avoid this notice?
Of course I could do something like
if(!is_null($booking->guest)) {
if($booking->guest->first_name == 'John') {}
}
But I really don't like this syntax which is too long. What would be recommended in that case?
Many thanks,
Felix
if ($booking->guest && $booking->guest->first_name == 'John') {}
Please or to participate in this conversation.