163

I love doing this sort of thing in Perl: $foo = $bar || $baz to assign $baz to $foo if $bar is empty or undefined. You also have $foo ||= $bletch which will only assign $bletch to $foo if $foo is not defined or empty.

The ternary operator in this situation is tedious and tiresome. Surely there's a simple, elegant method available in PHP?

Or is the only answer a custom function that uses isset()?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Tom Auger
  • 19,421
  • 22
  • 81
  • 104
  • 1
    Related on [Codereview.SE]: http://codereview.stackexchange.com/q/12722/31433 – Palec Aug 08 '15 at 01:28
  • 1
    BTW the Perl operators with the desired functionality are `//` and `//=` and they exist as of Perl v5.10.0. The original `||` and `||=` test for logical value, not for definedness. – Palec Aug 08 '15 at 01:33
  • 1
    @Palec, why would a 4-year old question with 29 upvotes be identified as a duplicate of a 1-year old question with 6 upvotes (which itself was marked as a duplicate of another question?) I think there's great value in keeping this question, as the title is more generic (doesn't reference the answer ie: isset()). – Tom Auger Aug 10 '15 at 17:24
  • They’re clear and exact duplicates. It admit I did not think much about which one should be the original, I was in the middle of something else, making a link between the two was the goal. Retracting my VTC. – Palec Aug 10 '15 at 19:09
  • An exact duplicate, marked (IMO erroneously) as a duplicate of another question: [PHP shorthand for isset()?](http://stackoverflow.com/q/18603250/2157640) – Palec Aug 10 '15 at 19:11
  • your variable names are confusing me. Why you don't use "x" "y" "z" ...?? – Black Oct 19 '15 at 13:00
  • @EdwardBlack Actually, "foo", "bar", "baz" and (arguably) "bletch" are pretty common place in the programming world - about as common as "x", "y", and "z" in the context of giving example variable / function / method names. – Tom Auger Nov 03 '15 at 17:04

8 Answers8

237

PHP 5.3 has a shorthand ?: operator:

$foo = $bar ?: $baz;

Which assigns $bar if it's not an empty value (I don't know how this would be different in PHP from Perl), otherwise $baz, and is the same as this in Perl and older versions of PHP:

$foo = $bar ? $bar : $baz;

But PHP does not have a compound assignment operator for this (that is, no equivalent of Perl's ||=).

Also, PHP will make noise if $bar isn't set unless you turn notices off. There is also a semantic difference between isset() and empty(). The former returns false if the variable doesn't exist, or is set to NULL. The latter returns true if it doesn't exist, or is set to 0, '', false or NULL.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 2
    +1 For introducing me to yet another great feature of 5.3 I'm missing out on in my RHEL5/PHP5.1.2 servers. – Michael Berkowski May 12 '11 at 02:10
  • I guess you mean `The first returns` instead of `The second` in your penultimate sentence. – Toto May 12 '11 at 08:16
  • 23
    Note that if your variable is undefined, PHP will throw notices about it. This, unfortunately, is no replacement for `$var = isset($var) ? $var : 'default value';` It says that in the answer... just pointing it out again for anyone that skims it. :-D – Brad Jun 02 '14 at 15:00
  • 6
    Is it bad to do $var = @$var ?: 'default value'; If so, why? Given that the only "error" could be that $var isn't set, and that we don't care if $var isn't set... – Codemonkey Nov 13 '15 at 14:14
177

In PHP 7 we finally have a way to do this elegantly. It is called the Null coalescing operator. You can use it like this:

$name = $_GET['name'] ?? 'john doe';

This is equivalent to

$name = isset($_GET['name']) ? $_GET['name']:'john doe';
jpschroeder
  • 6,636
  • 2
  • 34
  • 34
  • 15
    I'd argue the [spaceship operator](http://php.net/manual/en/migration70.new-features.php#migration70.new-features.spaceship-op) has merit too. – Mariano Jun 11 '17 at 05:41
  • 4
    I thought Mariano was pulling our legs but nope, it's a thing `<=>` and pretty accurate to boot! – HPWD Jun 14 '18 at 23:32
  • 2
    Note that the null coalescing operator behaves differently from the conditional operator, as it is specific to a `null` value. For example, if `$_GET['name']` is set to an empty string, the first line would return an empty string, but we could return "john doe" by using `$_GET['name'] ? $_GET['name'] : 'john doe'`. – VPhantom May 20 '20 at 18:33
  • @Mariano yeah but how is the spaceship operator relevant here ? – Xsmael Jun 18 '21 at 10:17
  • This answer is misleading/incorrect. See VPhantom's comment as to why this answer is harming the researcher experience. – mickmackusa Nov 01 '22 at 13:03
10

In PHP earlier than 7.*, one may use ?: for an undefined variable having errors locally suppressed with an @:

$foo = @$bar ?: $baz;
AndreyS Scherbakov
  • 2,674
  • 2
  • 20
  • 27
8

Thanks for all the great answers!

For anyone else coming here for a possible alternative, here are some functions that help take the tedium out of this sort of thing.

function set_if_defined(&$var, $test){
    if (isset($test)){
        $var = $test;
        return true;
    } else {
        return false;
    }
}

function set_unless_defined(&$var, $default_var){
    if (! isset($var)){
        $var = $default_var;
        return true;
    } else {
        return false;
    }
}

function select_defined(){
    $l = func_num_args();
    $a = func_get_args();
    for ($i=0; $i<$l; $i++){
        if ($a[$i]) return $a[$i];
    }
}

Examples:

// $foo ||= $bar;
set_unless_defined($foo, $bar);

//$foo = $baz || $bletch
$foo = select_defined($baz, $bletch);

I'm sure these can be improved upon.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Tom Auger
  • 19,421
  • 22
  • 81
  • 104
7

A common idiom to stay compatible with older PHP versions is:

 $var = $bool   or   $var = "default";
 // If I use it, then only with excessive spaces for clarity.

This works for values that can be evaluated in boolean context. The advantage here is that it also gives you said debug e_notice should the variable be undefined.

mario
  • 144,265
  • 20
  • 237
  • 291
  • Is a notice not emitted with `$bool ? $bool : "default"` if `$bool` isn't defined? – BoltClock May 12 '11 at 02:03
  • Sure, that's the same. I assumed OP is referring to `isset($var) ? $var : DEFAULT`. But sounds like he wants to shun them anyway. – mario May 12 '11 at 02:05
0

this is another good format for the isset case

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

another simple way and will give you more control as you can add more conditions and assign to another variable in the same time

$foo = (isset($oData['foo']))?$bar['foo']:'default value';

Bassem Shahin
  • 656
  • 7
  • 13
0

A possible solution: defaultFor( )

Unless we have a factory solution (which is indeed very annoying), I'd recommend the following little helper. It does the job in most cases:


    function defaultFor(&$x,$default=null) {
        if(!isset($x)) $x = $default;
    }

    //--------------------  Now you can do this: --------------

    defaultFor($a,"Jack");  // if $a is not set, it will be "Jack"
    defaultFor($x);         // no more notices for $x but keep it !isset

I hope this is close to what you wanted to achieve. It will not give you any notices if you use it with a nonexistent variable, and it's quite convenient. Surely it has a drawback: the default value always gets calculated beforehand so don't use it with anything heavy as a second parameter, like a file_get_contents or something. In those cases, you're better off with isseting.

dkellner
  • 8,726
  • 2
  • 49
  • 47
0

I think in general doing something like this:

$foo = $bar || $baz;

is a bad idea, unless $bar and $baz are both booleans. If they aren't:

$bar = 10;
$baz = 11;

then the question becomes: what determines if something is true or false? Most people would probably expect zero to be false, and everything else to be true. But with some languages (for example Ruby), only false and nil are false, which means both 0 and 1 would be true. Because of this cross language ambiguity, I think it best to be explicit in these cases:

if ($bar !== 0) {
   $foo = $bar;
} else {
   $foo = $baz;
}

or:

$foo = $bar !== 0 ? $bar : $baz;
halfer
  • 19,824
  • 17
  • 99
  • 186
Zombo
  • 1
  • 62
  • 391
  • 407