Tuesday, November 8, 2011

PHP: Ternary Operator, Short 'if' Statement, One Line 'if' Statement

The ternary operator is called so because it takes three operands; a condition, a result for true, and a result for false.

The ternary operator is a shorthand way of writing if statements.

Here's an example:

//Assign a string value to $x
$x = ($myvalue == 10) ? "the value is 10": "the value is not 10";

Breaking it down into its three parts you get this:
Condition: ($myvalue == 10)
Result for true: $x = "the value is 10"
Result for false: $x = "the value is not 10"

0 comments:

Post a Comment

Delicious Digg Stumbleupon Favorites