I've got some three values like this:
$a = null
$b = 3;
$c = null
(I never know what will be null: maybe all of them, maybe none of them.)
Following so called lazy loading, I've tried to assign the first not-null value this way:
$d = $a or $b or $c;
(It is similar to JavaScript way var d = a || b; (it will assign b if there is no a).)
But in PHP it seems to not work.
Am I doing it wrong, or what is best and simplest way to do this?
You can use the short ternary operator in PHP 5.3+:
$d = $a ?: $b ?: $c;
Note that this does type coercion like in JS.
Update (PHP 7)
In PHP 7.0+ you would do (called null coalescing - more informations):
$d = $a ?? $b ?? $c;
Try this...
$d = array_filter(array($a, $b, $c))[0]; //newer PHP only
or this:
$d = current(array_filter(array($a, $b, $c))); //warning about references
or this:
$tmp = array_filter(array($a, $b, $c));
$d = current($tmp); //most safe
In PHP 7 you can use the "Null coalescing" operator:
$d = $a ?? $b ?? $c;
This will assign the first non-null value (or null if there isn't one), as the question asked.
Unlike some of the other answers it won't be tripped up by implicit type casting so, for example,
$b = 0; $c = 1;
$d = $a ?? $b ?? $c;
echo $d;
will output 0: It won't mind that $a hasn't been set at all and won't pass over $b even though it type-casts to false.
If you can put variables in an array this can help:
$d = current(array_filter(array($a, $b, $c)));
or this can be a apporach as well:
if(!empty($a)) {
$d = $a;
}
this check may conitnue for all the variables like $b and $c
You can try with:
$d = !is_null($a) ? $a : (!is_null($b) ? $b : $c);
in php you can do it like this:-
`<?php
$a = 1;
$b = null;
$c = null;
$d = $a ? $a:($b?$b:($c?$c:'null'));
echo $d;
?>`
Related
Like
(true ? $a : $b) = 5;
Or
(true ? &$a : &$b) = 5;
Or
&(true ? $a : $b) = 5;
("It looks like your post is mostly code; please add some more details.")
Make it like this.
$val = 5;
true ? $a = $val : $b = $val;
You can achieve quite the same result using, instead of the variable itself as return value of the ternary expression, the name of the variable.
(true ? $a : $b) won't return the variables, but their content.
This can be achieved like this :
<?php
${true ? "a": "b"} = 5;
echo $a; // outputs 5
No, in PHP, all the 3 codes give syntax error.
Is there some short way to do this
if (!empty($b))
$a = $b;
else if (!empty($c)) {
$a = $c;
i know you could use ternary operator but its not what i asking like in JavaScript there is way to assign like this
my_var = some_Var || fu_bar || 0;
so if first dont exist it uses second and if second dont exist it uses third one.
is there similar thing in php?
cant think of other way than this:
$a = ! empty($b) ? $b : (! empty($c) ? $c : 0)
you can use one of the two ways whatever suits you. both of these functions will check either we have the non empty variable or not.
1: $a = (isset($b) && $b)?$b:$c;
2: if($b)
$a = $b;
elseif($c)
$a = $c;
Is
$a = 1;
$b = $a;
equal to writing this?
$a = $b = 1;
Will the second example always put 1 as value to both $a and $b, even if $a and $b already has a value assigned to them?
Quoting the documentation:
The value of an assignment expression is the value assigned. That is,
the value of "$a = 3" is 3. This allows you to do some tricky things:
<?php
$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.
?>
So, to answer your question, the result of the assignment $b = 1 is 1, and therefore, $a = $b = 1 would assign the value of $b = 1--which is to say 1--to $a.
That being said, abusing this can lead to code that is hard to read.
Yes, PHP will put 1 in $b then put $b value in $a, i.e. 1.
There is no ambiguity as the first assignment is $b = 1, the next is $a = $b.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Coalesce function for PHP?
I'm not sure what this is normally called, but I hope the title communicates well enough. What I have is a handful of variables some of which might be null.
I want to do:
$a = $b || $c || $d;
Where $a ends up being = to the first non-null variable.
To my knowledge, PHP doesn't support this in the same way JavaScript does.
You can, however do something like this:
$a = $b ? $b : ($c ? $c : $d);
A more general solution:
function fallthrough($arr) {
//$arr should be an array of possible values. The first non-null value is returned
do $a = array_shift($arr);
while($a === null && $arr);
return $a;
}
<?php
$a = 0;
$b = false;
$c = true; //should become this
$d = '1';
$e = $a ?: $b ?: $c ?: $d;
var_dump($e);
//bool(true)
//should be '1' if order is different
$e = $a ?: $b ?: $d ?: $c;
var_dump($e);
//string(1) "1"
... however ?: is kinda new, you will confuse your colleagues / fellow coders.
I don't think that's possible. I think you'd have to use some other, more laborious, way. I.e. make an array of the variables, iterate through it until you find a non-null value and break the loop, like so:
$vars = array("b" => $b, "c" => $c, "d" => $d);
foreach($vars as $var) {
if($var != null) {
$a = $var;
break;
}
}
Well, like some other answers here say, you can use the shorthand way of writing this, but writing readable code is important too. The above code is pretty readable.
I have two variables in PHP, say $a and $b. $a is a string variable. It contains $b. I want to update $a automatically if $b is updated.
$b = 4;
$a = "value is ".$b;
echo $a; // value is 4
$b = 5;
echo $a; // should print value is 5
Yes, $a can be updated automatically if you assign $b to $a by reference, but there should not be any string concatenation assigned to $a.
Try:
$b = 4;
$a = &$b;
$c = 'Value is ';
echo $c.$a;
$b = 5;
echo $c.$a;
Here is a demo
Not possible the way you want it. You see, variables can be passed by reference, like so:
$a = &$b;
Which will cause $a to automatically update when $b changes, however, it may not contain any other value, (like the string you want), so you'll have to use a function or another variable to do it.
$b = &$a;
echo "Value is $b";
or
$b = &$a;
$description = "Value is ";
echo $description . $b;
PHP doesn't have that feature. Related features you could use are:
References, which let you alias one variable to another. The value of each variable is the same, since they're simply symbol table aliases.
$b = "I'm b."
$a =& $b;
echo $a;
Variable variables, in which one variable holds the name of the other.
$b = "I'm b."
$a = 'b';
echo $$a;
However, variable variables should generally be avoided as they generally cause needless obfuscation.
Functions (as mithunsatheesh suggests). This is closest to what you want, as a function call is an expression that will have the value you're looking for. The only place a function wouldn't work where a variable would is when interpolating the value into a double-quoted string or a heredoc. Instead, you'd have to use string concatenation, or assign the result of the function call to a local variable and interpolate that.
You should pass it by reference. How to do it ?
Make a function:
function showValue(&$b)
{
return 'value is ' . $b;
}
echo showValue($b);
I think this should work.
Take a look at http://www.php.net/manual/en/language.references.whatdo.php
$a = 4;
$b =& $a;
$a = 5;
echo $b; // should print 5;
When a php script runs it runs "line after line". When you assign like this
$b = 4;
$a = "value is ".$b;
Value of $b is already assigned to $a as a integer 4 (not $b). So, if next $b is updated to some other value. Variable $a has no idea about it.
In this kind of case you have to use function or variable reference as describe in some other answers
$a = 4;
$b =& $a;
$a = 5;
echo $b;