mardi 10 juillet 2018

PSR-2 variable initialization in IF condition

The getUser() return User object if user is found else return null.

    // normal way
    $user = $this->getUser($id);

    if(empty($user)){
     echo 'User not found';
    } else {
     echo $user->name;
    }

Or can we do the following

if($user = $this->getUser($id)){
      echo $user->name;
    }else{
       echo 'User not found';
    }

Aucun commentaire:

Enregistrer un commentaire