dimanche 20 mars 2016

PHP Shorthand ifelse understanding

I am trying to make some sense out of the following shorthand

 $state = $account->getCity() ? $account->getCity()->getStates() : null;

Is my understanding right?

if $account->getCity() exist assign the value of $account->getCity()->getStates() to the $state variable else assign is null

Reason I am asking this is because I m getting the error

Error: Call to a member function getCity() on a non-object

If the code $state = $account->getCity() ? $account->getCity()->getStates() : null; does not check to see if $account->getCity() is null then how do I make it so it checks it.

The output I am trying to achieve is

if $account->getCity() is not null assign the value of $account->getCity()->getStates() to the $state else just make it null

Hopefully I am not sounding too confusing :)

Aucun commentaire:

Enregistrer un commentaire