What is this assignment construct called? And can you do it in Php?
Asked Answered
A

5

3

I have often used the following construct in Javascript:

var foo = other_var || "default_value";

In Javascript, if the left side is falsy, then the value on the right side is assigned.

It is very handy, and saves writing longer and unnecessarily explicit ternary expressions.

Is there a name for this sort of construct ?

Bonus: Is there a trick to do this in Php without using a ternary operator?

PS: another variant is to throw an error if you don't get a truthy value, instead of giving a default value:

var foo = something || alert("foo is not set!");
Axiology answered 15/11, 2010 at 17:56 Comment(3)
Thanks all for your answers, had to pick one but all are correct. Of course it is the logical OR :) Perhaps the word "pattern" would have better conveyed my puzzled observation. It is so handy that I thought people may have put a name on it. To me this is almost like a pattern. It is very readable and intuitively easy to understand. It is almost like reading plain english. The trick though as observed by Kos, is that it is entirely dependent on how the language evaluates the logical OR. Shame.Axiology
Another example. It is often useful to make sure that a non-acceptable result of a function returns false (or null, etc). But maybe the code gives a 0 or null (perhaps another routine you call from a underlying framework). So you end up with something like return var_result || false; If the result of the code is truthy it is returned as is, otherwise you are sure to return false as per documentation, or for consistency in your api. But yeah, it can be done with a ternary operator, only far less cool.Axiology
possible duplicate of Does PHP have a default assignment idiom like perl?Loveless
N
2

The logical-or (usually ||) operator is drastically different in many languages.

In some (like C, C++) it works like: "Evaluate the left-hand side; if it's true, return true, otherwise evaluate the right hand-side and return true if it's true or false otherwise." The result is always boolean here.

In others (like Javascript, Python, I believe that PHP also) it's more like: "Evaluate the left-hand side; if it's true, return it, otherwise evaluate the right-hand side and return the result." Then the result can be of any type and you can do constructs like:

a = (b || c); // equivalent to a = b ? b : c;

or quite fancy:

function compare(A, B) { // -1 if A<B, 0 if A==B, 1 if A>B
    return B.x - A.x || B.y - A.y;
}
Northeast answered 15/11, 2010 at 18:19 Comment(1)
Accepting your answer (though all answers are correct) because it was more relevant to my observation. The different treatment of the operator is very annoying. I wonder if there is any reason at all to have it NOT operate as it does in Javascript. Since I spend a lot of time in front end code, this is a pattern I have used many times. Of course a ternary operator works, but the way it is written with a logical OR makes the code more readable and sort of intuitively written down.Axiology
B
1

I believe it's just called an OR construct. There are a lot of good examples on using assignments here: http://php.net/manual/en/language.operators.assignment.php

Bacolod answered 15/11, 2010 at 18:0 Comment(0)
M
1

It is just a logical OR operator. Follow the link for more information in javascript.

From the examples in the docs:

o1=true || true       // t || t returns true
o2=false || true      // f || t returns true
o3=true || false      // t || f returns true
o4=false || (3 == 4)  // f || f returns false
o5="Cat" || "Dog"     // t || t returns Cat
o6=false || "Cat"     // f || t returns Cat
o7="Cat" || false     // t || f returns Cat

EDIT: Regarding the BONUS, it appears as though you can do something similar with the ternary in recent versions of PHP by doing:

expr1 ?: expr3

From the PHP docs:

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

I'm not familiar with PHP, so I'd be interested to know the result.

Monocyte answered 15/11, 2010 at 18:2 Comment(2)
Thanks for the bonus! I confirm that it works with a simple one-liner in the shell (php -r 'echo (null?:"dog") . "\n";' echoes "dog"), on PHP 5.3.2; and not with PHP 5.2.14. Falsy values appear to be evaluated just like the php empty() function, so an empty string is falsy, but a string containing the zero character alone, is also falsy.Axiology
@faB - Interesting. I don't know a thing about PHP (beyond what I read in the docs today). Surprises that it would consider "0" to be falsey, but I guess I'm just accustomed to javascript.Monocyte
R
1

This works:

$mytruevalue = true;
$foo = $mytruevalue or $foo = "20";
echo $foo;

The above prints "1" because that is the string representation of true ($mytruevalue is true).

$myfalsevalue = false;
$foo = $myfalsevalue or $foo = "20";
echo $foo;

This, however, prints "20" because $myfalsevalue is false.

If both values are equal to false, it prints nothing.

Hope this helps.

Rhinarium answered 15/11, 2010 at 18:16 Comment(2)
Oh I see. Hehe. Well that is neat, the right side is an assignment. But I'm not sure that's more desirable than a plain ternary operator. It may also lead to more stealthy errors because of the order of evaluation, with that extra assignment there. You got me confused a moment because of the plain english version of the logical operator.Axiology
Actually, both sides are assignments. I completely agree, though, the ternary operator is the best choice for PHP.Rhinarium
H
0

Is there a name for this sort of construct ?

It is the logical OR operator.

Bonus: Is there a trick to do this in Php without using a ternary operator?

No, with PHP, you can do so only with the help of ternay operator.

Homegrown answered 15/11, 2010 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.