Shorthand If/Else (ternary) Statements - C#, C++, Python & PHP

Posted on Dec 5, 2016

I’ve been going through the multiple languages I know recently, and refreshing my knowledge. After working with Python for a few months, I found going back to C# very easy.

I’ve noticed many similarities except for a few; In my opinion the ternary operator slightly differs from each language. So I’d thought I’d write these findings down for future use.

C# & C++

variable = (input > 0) ? ’true’ : ‘false’;


variable = ’true’ if input > 0 else ‘false’


$variable = ($input > 0) ? ’true’ : ‘false’;