vendredi 17 novembre 2017

under the hood execution of if condition in PHP

Following block of code throws an exception that variable $booking is undefined. can someone explain why?

if(
     trim($request->id)!='' 
     && $booking = Booking::find($request->id) 
     && $booking->user_id == auth()->user()->id // undefined variable $booking
       ) {
          return response()->json($booking);
    }
    else {
          return response()->json(['message' => 'not found'],404);
    }

However this works without any issue

if(
    trim($request->id)!='' 
    && $booking = Booking::find($request->id)
    ) {
      if($booking->user_id != null && $booking->user_id == auth()->user()->id)
                    return response()->json($booking);
}
return response()->json(['message' => 'not found'],404);

Aucun commentaire:

Enregistrer un commentaire