+ 6

$var=true?1:false?2:3; ??

Hi, i found something like this while doing challenges. Can anyone explain how i do this calculation and what the name of it is?

21st Nov 2018, 12:50 PM
Arne Van Kerckvoorde
Arne Van Kerckvoorde - avatar
4 Answers
+ 5
This is using the ternary operator 😉 its like a shorthand way to using if and else statements your result would be 1 as the first condition is true if it was false you would get 3 as your conditional statement before last returned false. Your example behind the scene looks like this condition ? exprTrue : exprFalse?exprTrue:exprFalse;
21st Nov 2018, 2:04 PM
D_Stark
D_Stark - avatar
+ 3
Thank's for the answers 😄
21st Nov 2018, 4:34 PM
Arne Van Kerckvoorde
Arne Van Kerckvoorde - avatar
+ 2
https://www.sololearn.com/learn/Swift/2338/ See ternary operator example here
21st Nov 2018, 2:05 PM
Roneel
Roneel - avatar
+ 1
This is called Ternary Expression. (Ternary Operator : An expression a ? b : c evaluates to b if the value of a is true, and otherwise to c) $var=true?1:false?2:3; $var=True?1:false; // first this expression evaluated Then $var=1; $var=1?2:3; // second this expression evaluated Then $var=2; https://code.sololearn.com/wBv0RrjcQGVO/#php
21st Nov 2018, 2:07 PM
Prokopios Poulimenos
Prokopios Poulimenos - avatar