Snippet Name: Ternary Operator
Description: Included for the sake of reference.
Few PHP programmers use the ternary operator syntax. It's more difficult to read and has no real advantage over the traditional If/Else syntax. We recommend not using it.
Comment: (none)
Language: PHP
Highlight Mode: PHP
Last Modified: March 07th, 2009
|
// traditional if/else syntax
IF (EMPTY($_POST['action'])) {
$action = 'default';
} ELSE {
$action = $_POST['action'];
}
// This example of a ternary operator will produce the
// same result as the previous example using less space.
// It makes use of ? and : just like if and else.
$action = (EMPTY($_POST['action'])) ? 'default' : $_POST['action'];
|