21

Firstly is there a name for this expression ?

Javascript

var value = false || 0 || '' || !1 || 'string' || 'wont get this far';

value equals string (string) aka the fifth option

PHP

$value = false || 0 || '' || !1 || 'string' || 'wont get this far';

$value equals true (bool)

Am I right in thinking the correct way to achieve the same result as JavaScript is by nesting ternary operators? What is the best solution ?

9
  • 1
    It is true only..Test echo true; Commented Apr 6, 2016 at 12:04
  • You might wanna check this out. Commented Apr 6, 2016 at 12:04
  • 3
    In JS they’re called short-circuit evaluation Commented Apr 6, 2016 at 12:05
  • 1
    <?php $value = false || 0 || '' || true || 'wont get this far'; echo $value===true?'true':$value; ?> Commented Apr 6, 2016 at 12:06
  • @BlazeSahlzen thanks Knowing the correct terminology helps a lot. Commented Apr 6, 2016 at 12:10

4 Answers 4

31

The equivalent operator in PHP is ?:, which is the ternary operator without the middle part:

$value = false ?: 0 ?: '' ?: !1 ?: 'string' ?: 'wont get this far';

$a ?: $b is shorthand for $a ? $a : $b.

Sign up to request clarification or add additional context in comments.

6 Comments

I think the question is not about ternary operator. But OP confusedly called ternary operator instead of short-circuit evaluation.
In Javascript || returns one of its operands, not a boolean. The equivalent way to do that in PHP is ?:. Question answered.
Thanks for the explanation. I was assuming something else.
OP did not get confused, OP merely suggested using a ternary to achieve the result.
Just realized.. and deleted my answer to support this answer.
|
2

If You are using PHP 5.3 or higher see deceze's answer.

Other wise you could use nested regular ternary operators.

$value = ( false ? false : ( 0 ? 0 : ( '' ? '' : ( !1 ? !1 : ( 'string' ? 'string' : ( 'wont get this far' ? 'wont get this far' : null )))))); 

Wow thats ugly.

You could use an array of values instead;

$array = array(false,0,'',!1,'string','wont get this far'));

Now create a function which iterates over the array and returns the first true value.

function array_short_circuit_eval($vars = array()){
    foreach ($vars as $var)if($var)return $var;return null;
}

$value = array_short_circuit_eval($array);

echo $value; // string

1 Comment

$value = current(array_filter(array(false, 0, ...))) – I hope nobody needs to use this, but here it is...
1

This test false || 0 || '' || !1 || true || 'wont get this far' will return a boolean value. It will return true if any of the values is true, that's how the OR works. It's not a ternary expression, which applies the first valid value to the receiving variable.

It returns 1 to PHP because you didn't cast the expression as a boolean.

You could do this to make the expression return a boolean value instead of an integer into your PHP variable:

$value = (bool)(false || 0 || '' || !1 || true || 'wont get this far');`

The return will be true.

3 Comments

".. || .. will return a boolean value...", "You could do this to make the expression return a boolean value..." – Which is it? Does || return a boolean or doesn't it?
It does return a boolean, but not directly into a variable, no in PHP. I don't get the two downvotes, but alrighty
Wut? Either it returns a boolean or it doesn't. There's no difference whether you assign the return value into a variable or not. A value is a value. The boolean return value of || won't get any more boolean by casting it.
0

Another approche with 'or' and '=' operators.

example:

in Javascript:

$value = false || 0 || !1 || 'woow, i am selected';

in Php:

$value = false or $value = 0 or $value = !1 or $value = 'woow, i am selected';

This is possible because in Php 'or' operator has lower precedence order then '=' (assignment).

1 Comment

@NicoHaase Its not a duplicated answer, this answer is using the combination of operators (or, =) and provides a correct result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.