24

In PHP I often write lines like

isset($foo)? NULL : $foo = 'bar'

In ruby there is a brilliant shortcut for that, called or equals

foo ||= 'bar'

Does PHP have such an operator, shortcut or method call? I cannot find one, but I might have missed it.

Community
  • 1
  • 1
berkes
  • 26,996
  • 27
  • 115
  • 206
  • 5
    I just want to point out that the two examples are not equivalent. `foo ||= 'bar'` in Ruby is more equivalent to `isset($foo) && !($foo === false || $foo === NULL) ? NULL : $foo = 'bar'` in PHP. – Jörg W Mittag Jul 29 '10 at 13:21
  • Thank you for pointing that out. Indeed the edge cases where $foo contains "false" or null are not covered in my PHP code. – berkes Jul 29 '10 at 14:02

8 Answers8

23

As of PHP7, you can use the Null Coalesce Operator:

The coalesce, or ??, operator is added, which returns the result of its first operand if it exists and is not NULL, or else its second operand.

So you can write:

$foo = $foo ?? 'bar';

and it will use $foo if it is set and not null or assign "bar" to $foo.

On a sidenote, the example you give with the ternary operator should really read:

$foo = isset($foo) ? $foo : 'bar';

A ternary operation is not a shorthand if/else control structure, but it should be used to select between two expressions depending on a third one, rather than to select two sentences or paths of execution

Gordon
  • 312,688
  • 75
  • 539
  • 559
9

I really like the ?: operator. Unfortunately, it is not yet implemented on my production environment. So, if I were to make this look ruby-ish, I would go for something like:

isset($foo) || $foo = 'bar';

Or, if you want it even shorter (slower, and may yield unexpected results):

@$foo || $foo = 'bar';
Ethan Kent
  • 271
  • 3
  • 2
5

I find it readable, concise and performant to just do:

isset($foo) or $foo = 'bar';
raveren
  • 17,799
  • 12
  • 70
  • 83
5

You could create your own function:

function setIfNotSet(&$var, $value) {
    if(!isset($var)) {
        $var = $value;
    }
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
3

As of PHP 5.3 it's possible to use $foo ?: 'bar' Unless you expect $foo to be false

[edit]

Forget it. It still raises E_NOTICE if $foo is no set.

Mchl
  • 61,444
  • 9
  • 118
  • 120
  • It's a real pity that this feature was implemented only since php 5.3 – Kirzilla Jul 29 '10 at 12:59
  • 2
    @Paul Dragoonis, nope. ?: returns the first part or the second if false. `$foo = isset($foo) ?: 'bar';` would set `$foo` to `true` if it is set, or 'bar' if it is not... It's a great oversight in ternary usage... – ircmaxell Jul 29 '10 at 13:28
  • I seen the PHP RFC wiki post for this inclusion into PHP 5.3 although i've never used it for 5.2 backwards compatability. Stick to good ol' fashion ternary :) – Paul Dragoonis Jul 29 '10 at 15:03
2

From the manual:

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

It's not exactly the same though. Hope it helps anyway.

kschaper
  • 309
  • 1
  • 6
1

The most similar with ruby is this:

$foo or $foo = 'bar';

$foo is false if

$foo = 0;
$foo = '0';
$foo = NULL;
$foo = '';
$foo = array();
$foo = FALSE;
  • `isset($foo) or $foo = 'bar'` is equal at `$foo === NULL and $foo = 'bar'` – user3788358 Jun 29 '14 at 19:24
  • not quite, isset() tests if $foo is null OR if it was ever assigned a value (putting it in an equality throws an error!), but if it were ```!== NULL``` or something that may be true. – Ryan Taylor Jun 17 '15 at 21:27
1

No. According to w3schools, that operator doesn't exist.

Also, the PHP code you posted is rather cryptic. I prefer something like this:

if (!isset($foo)) $foo = 'bar';
Scott M.
  • 7,313
  • 30
  • 39