2

Is my return missing something?

return $var === 'apple' ? 'A' : $var === 'banana' ? 'B' : 'C';

There is only one issue is when $var is ='apple' is doesn't return A it returns B

I can definitely see the $var printing apple So why not returning A

For me the statement is correct anyone have any idea?

6
  • I'd 100% avoid elseif ternary statements.. Commented Jul 18, 2019 at 10:31
  • @treyBake Explain Why! Commented Jul 18, 2019 at 10:32
  • a thing called readability Commented Jul 18, 2019 at 10:33
  • @treyBake Understand But this is very short And an experience developer would not have problem with it. But the question still remains. Commented Jul 18, 2019 at 10:34
  • An experienced developer would probably not use a elseif ternary Commented Jul 18, 2019 at 10:35

1 Answer 1

2

You need to use () in second condition like below:

return $var === 'apple' ? 'A' : ($var === 'banana' ? 'B' : 'C');

Output:-

https://3v4l.org/tIFGH

https://3v4l.org/rtaAE

https://3v4l.org/ZGUQW

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.