The best way to set a variable to its default value (simulate Perl ||, || =)

I like to do such things in Perl: $foo = $bar || $baz $foo = $bar || $baz assign $baz to $foo if $bar empty or undefined. You also have $foo ||= $bletch , which assigns $bletch $foo if $foo is undefined or empty.

The ternary operator in this situation is tiring and tiring. Surely there is a simple, elegant method available in PHP?

Or is this the only answer to a user-defined function that uses isset ()?

+102
operators php perl default-value
May 12 '11 at 1:50 a.m.
source share
6 answers

PHP 5.3 has an abbreviated operator ?: ::

 $foo = $bar ?: $baz; 

What does $bar assign if it is not an empty value (I don’t know how it would be otherwise in PHP from Perl), otherwise $baz is the same as in Perl and older versions of PHP:

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

But PHP does not have a complex assignment operator for this (i.e. no equivalent to Perl ||= ).

In addition, PHP will make noise if $bar not set, unless you turn off notifications. There is also a semantic difference between isset() and empty() . The first returns false if the variable does not exist or is set to NULL . The latter returns true if it does not exist, or set to 0 , '' , false or NULL .

+190
May 12 '11 at 1:52
source share

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

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

It is equivalent

 $name = isset($_GET['name']) ? $_GET['name']:'john doe'; 
+85
Apr 22 '16 at 2:48
source share

Thanks for the great answers!

For everyone who comes here for a possible alternative, here are some features that help bring boredom out of this kind 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 am sure that they can be improved.

+8
May 12 '11 at 2:40 pm
source share

Common idiom for compatibility with older versions of PHP:

  $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 a boolean context. The advantage here is that it also gives you the specified e_notice debugging if the variable is undefined.

+7
May 12 '11 at 1:59 a.m.
source share

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

 $foo = @$bar ?: $baz; 
+3
Oct 28 '17 at 11:23
source share

this is another good format for isset case

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

another easy way and will give you more control, since you can add more conditions and assign another variable at the same time

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

0
Jun 18 '19 at 22:34
source share



All Articles