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!
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 .
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.
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.
You cannot conditionally return from the middle of an expression, because return is a statement, you have to return the whole expression.
This is the correct syntax:
return !$e ? '' : array('false', $e);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With