PHP Ternary Operator - Conditional Shorthand
I absolutely love the ternary operator and use it throughout my code. Sometimes people call it shorthand conditional. It is a super-simple replacement for a basic if/else operation. Basically there are two possibilities dictated by one conditional as follows:
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
Example:
PHP:
Classic Way:
PHP:
-
$action = 'default';
-
}
-
else {
-
$action = $_POST['action'];
-
}
Home | PHP | PHP Ternary Operator - Conditional Shorthand
You’re currently reading “ PHP Ternary Operator - Conditional Shorthand ,” an entry on BRADINO
- Published:
- 3.26.07 / 4pm
- Category:
- PHP












