I've just started learning PHP yesterday, and in reading the PHP manual from php.net, I came across the Ternary operator. I found it very confusing and their definition lacking; could anyone do me the favor of explaining it in a little more detail. Thank you.
Essentially the ternary operator provides a 'simpler' if / else. So: $a = ( $b ? $c : $d ); translates to: if ( $b ) { $a = $c; } else { $a = $d; } Basically if the value before the question mark is true, set the value on the left hand side of the equal sign to whatever is between the question mark and the colon, otherwise set it to whatever is after the colon. $b can be anything that can be used as a boolean and $c and $d can be anything that could normally set a value to the left of an equal sign, so you can do some nifty things like calling functions depending on the true / false value of another function and so on.
More examples: <? //Validating a password: $username = "user123"; $password = "123456"; echo ($username == "user123" && $password == "123456") ? "Valid" : "Invalid"; ?> PHP: