2

Hi,

I saw somebody else asked this before but the answers there are not what I am looking for. I would like to know what is the shorthand for expressions like this example:

if ($gender == 'male'){ echo 'M; } else if($gender == 'female'){ echo 'F'; } else { echo 'undefined'; } 

I only know what to do when there is only 2 possibilities:

echo ($gender == 'male' ? 'M' : 'F'); 

but what about when you have else if like in my example above?

Thank you.

1 Answer 1

2

Check the variable again if the statement is false and you need to add parenthesis around the entire else block

echo ($gender == 'male') ? 'M' : (($gender == 'female') ? 'F' : 'undefined'); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Thats the answer I needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.