Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using return in ternary operator

I'm trying to use return in a ternary operator, but receive an error:

Parse error: syntax error, unexpected T_RETURN 

Here's the code:

$e = $this->return_errors();
(!$e) ? '' : return array('false', $e);

Is this possible?

Thanks!

like image 959
dzm Avatar asked Jun 07 '11 13:06

dzm


People also ask

Can I use return in a ternary operator?

The ternary operator isn't meant to execute an action or another but rather to yield a value or another. You can abuse it in much cases since most actions will return undefined or something more appropriate, but you can't do that with return .

Can we use return statement in ternary operator Python?

The ternary operator is used to return a value based on the result of a binary condition. It takes in a binary condition as input, which makes it similar to an 'if-else' control flow block. It also, however, returns a value, behaving similar to a function.

What is the return type of ternary operator in Java?

If the first operand is true then java ternary operator returns second operand else it returns third operand. Syntax of java ternary operator is: result = testStatement ? value1 : value2; If testStatement is true then value1 is assigned to result variable else value2 is assigned to result variable.

Can we use return in conditional statement?

You cannot conditionally return from the middle of an expression, because return is a statement, you have to return the whole expression.


1 Answers

This is the correct syntax:

return  !$e ? '' : array('false', $e);
like image 107
ioseb Avatar answered Oct 25 '22 18:10

ioseb