Warning about Shorthand Form of the Ternary Operator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Warning about Shorthand Form of the Ternary Operator

Ternary Operator ( expression1 ) ? expression2 : expression3; Has Obvious output but be carefull of the Shorthand Form of the Ternary Operator (From PHP 5.3 onwards, you can omit the second expression in the list) ( expression1 ) ?: expression3; This code evaluates to the VALUE OF expression1 if expression1 is true; otherwise it evaluates to the value of expression3. See Example: https://code.sololearn.com/wUcpXw0i2iPM

22nd Apr 2017, 1:35 PM
altergothen
altergothen - avatar
2 Answers
+ 9
thanks for your warning.
22nd Apr 2017, 1:36 PM
Agus Mei
Agus Mei - avatar
+ 9
C# has another: object1 ?? object2; if object1 is null then object2 will be returned instead of object1. It works like this. if(object1 != null) return object1; else return object2;
22nd Apr 2017, 1:42 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar