Reputation: 29872
I'm wanting to assign a variable only it hasn't already been assigned. What's the PHP way of doing the following?
$result = null;
$result ||= check1();
$result ||= check2();
$result ||= "default";
I checked the standard operators and the is_null function, but there doesn't seem to be an easy way of doing the above operation.
Upvotes: 7
Views: 997
Reputation:
I came up with this function which has a side benefit of shutting up useless undefined variable notices. It's close enough to Javascript's behaviour for me:
function default_value(&$var, $default) {
return isset($var) ? $var : $default;
}
$want_something = default_value($_POST['mightbeunset'], false);
Upvotes: 1
Reputation: 3606
I haven't tried it yet, but have wanted to play with PECL Operator for ages.
Upvotes: 0
Reputation: 9487
How about using a couple of nested ternary operators?
For me it's the easiest way to deal with your problem. And since you can use nested ternary operators you should be able to add more checks if needed.
$var = isset($var) ? $var : ($i=check1()) ? $i : ($i=check2()) ? $i : "default" ;
It's almost the same amount of code and it should work.. unless I've misunderstood you. If that's the case I'm sorry.
In comparison with the original code you posted as example, the length is almost the same:
$result = null; $result ||= check1(); $result ||= check2(); $result ||= "default";
- vs -
$var = isset($var) ? $var : ($i=check1()) ? $i : ($i=check2()) ? $i : "default" ;
Nested ternary operators can be a little bit confusing, so here you have it with comments:
// Is $var set?
$var = isset($var) ?
// TRUE: It is, use $var
$var :
// FALSE: $var is not set, run check1
( $i=check1() ) ?
// TRUE: the function check1 returned valid data
$i :
// FALSE: the function check1 returned null or false, run check2
($i=check2()) ?
// TRUE: the function check2 returned valid data
$i :
// FALSE: the function check1 returned null or false, set default value
"default" ;
Upvotes: 0
Reputation: 625087
isset()
is the usual way of doing this:
if (!isset($blah)) {
$blah = 'foo';
}
Note: you can assign null
to a variable and it will be assigned. This will yield different results with isset()
and is_null()
so you need to be clear on what you mean by "not assigned". See Null vs. isset(). This is also one case where you need to be careful about doing automatic type conversion, meaning using the !=/==
or ===/!==
depending on the desired result.
You can use boolean shorthand for this too (which is what the Perl ||=
operator is). As of PHP 5.2.x there is no operator like you speak of. In Perl:
$a ||= $b;
is equivalent to:
$a = $a || $b;
You can do the second form in PHP but PHP has some funky rules about type juggling. See Converting to boolean:
When converting to boolean, the following values are considered FALSE:
- the boolean
FALSE
itself- the integer 0 (zero)
- the float 0.0 (zero)
- the empty string, and the string "0"
- an array with zero elements
- an object with zero member variables (PHP 4 only)
- the special type
NULL
(including unset variables)- SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource).
So after:
$a = 0;
$a = $a || 5;
$a would equal 5. Likewise:
$a = 0;
$b = '';
$c = $a == $b; // true
$d = $a === $b; // false
You have to watch out for these things.
Upvotes: 17