Setting default values (conditional assignment)
In Ruby you can easily set a default value for a variable x ||= "default" The above statement will set the value of x to "default" if x is nil or false Is there a similar shortcut in PHP or do I have to use the longer form: $x = (isset($x))? $x : "default"; Are there any easier ways to handle this in PHP? As of PHP 5.3 you can use the ternary operator while omitting the middle argument: $x = $x ?: 'default'; isset($x) or $x = 'default'; As of PHP 7.0, you can also use the null coalescence operator // PHP version < 7.0, using a standard ternary $x = (isset($_GET['y'])) ? $_GET['y'] : 'not set';