Assign variable within condition if true
Solution 1
@chandresh_cool's suggestion is right but to allow multiple possiblities / fallbacks you would have to nest the ternary expressions:
$var = ($foo == true) ? $foo:
($bar == true) ? $bar:
($fuzz == true) ? $fuzz:
$default;
Note: the first 3 lines end in colons not semi-colons.
However a simpler solution is to do the following:
$var = ($foo||$bar||$fuzz...);
Solution 2
Although this is a very old post. Fallback logic on falsify values can be coded like this.
$var = $foo ?: $bar ?: "default";
In this case when $foo
is a falsified value (like false, empty string, etc.) it will fall back to $bar
otherwise it uses $foo
.
If bar is a falsified value, it will fallback to the string default
.
Keep in mind, that this works with falsified values, and not only true
.
example:
$foo = "";
$bar = null;
$var = $foo ?: $bar ?: "default";
$var
will contain the text default
because empty strings and null
are considered "false" values.
[update]
In php 7 you can use the new null coalescing operator: ??
, which also checks if the variable exists with isset(). This is usefull for when you are using a key in an array.
Example:
$array = [];
$bar = null;
$var = $array['foo'] ?? $bar ?? "default";
Before php 7 this would have given an Undefined index: foo
notice. But with the null coalescing operator, that notice won't come up.
Solution 3
Instead you can Use ternary operator like this
$var = ($foo == true)?$foo:"put here what you want";
Solution 4
You can assign values like this:
$var = $foo;
Setting them within an if statement is also possible, PHP will evaluate the resulting $var which you just assigned.
I dont really get your question, but you could do something like this:
if(!($var = $foo)){
//something else.
}
Comments
-
Jared almost 2 years
I know you can assign a variable in a condition like this:
if ($var = $foo)
However I don't need to do anything in the condition itself, so I'm often left with empty brackets. Can I simply assign
$var
if$foo
istrue
in some other way without needing to do something else later?Also can I assign
$var
to$foo
if$foo
istrue
but if$foo
isfalse
do something else? Like:if ($var = !$foo) { if ($var = !$bar) { //Etc... } }
Basically I want to have more fallbacks/defaults.
-
Jared almost 11 yearsYour simpler solution is exactly what I'm looking for. It's short, sweet and does what I need it to do: set $var to the first positive value it can find.
-
Jared almost 11 yearsIt's still a little long-winded, but it works so I'll give you +1 for your answer.
-
Jared almost 11 yearsYour if statement code looks like what I'm trying to do, but RobJ's answer was better. I'll still give you a +1 for it.
-
Veda over 9 yearsIf you're going for the first option, please don't do the "== true", just do "$var = ($foo) ? $....."
-
HPierce over 7 years@jconder - I'm rolling back your edit to this answer. Leroy says "otherwise it uses
$foo
", but the edit you've made completely invalidates that. Leroy is intentionally using two ternary operators here (each with the first expression being empty), allowing for 3 possible outcomes:$foo
,$bar
, or"default"
, rather than using a single ternary operator which could only return 2 possible outcomes:$bar
or"default"
. -
jconder over 7 yearsYep you're right about that, my bad, I missed the entire point of the post.
-
iyrin about 7 yearsThis makes if expressions a lot cleaner. I'm using this to check for a custom post type in WordPress
$is_entry = is_singular( 'myentry' ) ?: is_post_type_archive( 'myentry' ) ?: false;
. Now I can useif ( $is_entry && $other_condition )
to determine if the current page belongs to my post type without the if expression getting too ugly. -
Leroy about 7 yearsIn php7 there is a new null coalescing operator:
??
, which also checks if the variable exists likeisset()
. This is usefull for when you are using a key in an array. For example:$var = $array['key'] ?? $bar ?: "default"
. Now when thekey
in$array
does not exists, it won't throw any errors. Because it wil also look if thekey
exists.