Possible Duplicate: What are the PHP operators “?” and “:” called and what do they do?
From http://twitto.org/
<?PHP
require __DIR__.'/c.php';
if (!is_callable($c = @$_GET['c'] ?: function() { echo 'Woah!'; }))
throw new Exception('Error');
$c();
?>
Twitto uses several new features available as of PHP 5.3:
- The DIR constant
- The ?: operator
- Anonymous functions
What does number 2 do with the ?: in PHP 5.3?
Also, what do they mean by anonymous functions? Wasn't that something that has existed for a while?
?:
is a form of the conditional operator which was previously available only as:In 5.3 it's possible to leave out the middle part, e.g.
expr ?: val_if_false
which is equivalent to:From the manual:
Look here:
Anonymous functions: No, they didn't exist before 5.3.0 (see the first note below the examples), at least in this way:
The only way was
create_function()
, which is slower, quite cumbersome and error prone (because of using strings for function definitions).The
?:
operator is the conditional operator (often refered to as the ternary operator):In the case of:
The expression evaluates to the value of
expr1
ifexpr1
is true andexpr2
otherwise: