OUTPUT WITH EXPLANATION... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

OUTPUT WITH EXPLANATION...

<?php $var = true?'1':false?'2':'3'; echo $var; ?>

28th Jun 2019, 12:35 PM
🇳🇵Bed Narayan Rai
🇳🇵Bed Narayan Rai - avatar
3 Answers
+ 2
Sorry, I don't know why I didn't see it, but here's the explanation: You can look at it like this: (true? '1': false)? '2': '3'; In the parentheses, it will be 1, which is evaluated to true, therefore the output is 2
28th Jun 2019, 1:26 PM
Airree
Airree - avatar
+ 2
THE OUTPUT IS 1. THIS CODE USES THE TERNARY OPERATOR, WHICH WORKS LIKE THIS: condition? condition == true: condition == false; true IS OBVIOUSLY EVALUATED TO TRUE, SO IT WILL BE THE FIRST OPTION, WHICH IS 1
28th Jun 2019, 1:08 PM
Airree
Airree - avatar
+ 1
Airree but the output is 2. Why?
28th Jun 2019, 1:17 PM
🇳🇵Bed Narayan Rai
🇳🇵Bed Narayan Rai - avatar