Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign variable within condition if true

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 is true in some other way without needing to do something else later?

Also can I assign $var to $foo if $foo is true but if $foo is false do something else? Like:

if ($var = !$foo) {
    if ($var = !$bar) {
        //Etc...
    }
}

Basically I want to have more fallbacks/defaults.

like image 308
Jared Avatar asked May 16 '13 07:05

Jared


People also ask

Can you assign a variable in an if statement?

Yes, you can assign the value of variable inside if.

How do you assign an IF condition?

Can we put assignment operator in if condition? Yes you can put assignment operator (=) inside if conditional statement(C/C++) and its boolean type will be always evaluated to true since it will generate side effect to variables inside in it.

Can you set a variable in an if statement Python?

We can use a special operator to assign a variable within the expression of an if-statement. This is commonly called the "walrus operator" for its syntax. Same line. An if-statement can be on the same line as its body.

Can you declare a variable in an if statement JS?

Answer. No, variables declared inside an if statement would not be accessible directly outside an if statement because if statements have their own scope.


2 Answers

@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...);
like image 68
Rob Johnstone Avatar answered Nov 15 '22 21:11

Rob Johnstone


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.

like image 35
Leroy Avatar answered Nov 15 '22 22:11

Leroy