Skip to content

Instantly share code, notes, and snippets.

@philsturgeon
Created November 26, 2013 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save philsturgeon/7664724 to your computer and use it in GitHub Desktop.
Save philsturgeon/7664724 to your computer and use it in GitHub Desktop.
Proposed short logical assignment operator
<?php
$foo = null;
// If's - verbose and uneccassry
if (! $foo) {
$foo = 'default';
}
// Short Ternary syntax
$foo = $foo ?: 'default';
// Long Operator syntax (see, this is nothing new or alien to PHP, but still writing $foo twice)
$foo || $foo = 'default';
// Proposed Short Operator syntax (Ruby Style - Consistency issues raised, as all
// PHP assignment operators are formed as <op>= and equate to $x = $x <op> $y, while this is $x = $x <op> $y)
$foo ||= "default";
// Proposed Short Operator syntax (Avoids the issue by moving the operator to avoid confusion with other <op>=)
$foo =|| "default";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment