无法用三元运算符复制条件

I'm trying to replicate this condition:

$sth = $this->getResult();
if($sth !== true){
   return $sth;
}

with ternary operator I tried with;

($sth !== true) ?  return $sth : ($sth == true) ? null;

But I got:

Expected colon

What you're ultimately trying to achieve is not possible with a ternary operator.

What you try to do is ONLY return in 1 situation, and continue the code in the other. The only way you could do this using a ternary operator is like this:

$result = ($sth !== true) ? true : false;
if ($result) return;

But that kinda defeats the purpose of the ternary operator.


In fact, your code has a couple of problems:

  • Ternary operator always needs 3 parts (*)
  • There is no need to check a condition twice, as that's the entire point of the operator
  • The operator returns a value, so you can't put a return inside the truthy or falsy part.

A ternary operator needs 3 parts

(condition) ? truthy result : falsy result

Note: 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. - source: php.net

No need to check things twice:

Again, let's take our operator:

($sth !== true)
                ? "I return if '$sth' is not true"
                : "I return if '$sth' is true";

There is no need to have a 2nd check. You have both situations covered already. :)

No return values inside the truthy or falsy part

Finally: it is an operator that returns a value. You can't put a return in the truthy or falsy part, you need to put it in front of it:

return ($sth !== true)
                ? "I return if '$sth' is not true"
                : "I return if '$sth' is true";

You have two '?'s in your code, and only one ':'. I believe that's the matter. You must have the same amount of question marks as you have colons.

If you are using it to return from a function I would do it like so:

function testFunc()
{
    //arbitrary setting of $sth only to explain the point
    $sth = false;

    //condition ? true : false
    $outcome = ($sth === true) ? true : false;

    return $outcome;
}

// false
var_dump(testFunc());

Remember, (condition) ? value if true : value if false;

You can also miss out the [value if true] parameter, like so:

$outcome = ($sth === true) ?: false;

That expression would assign the value of $sth to $outcome if $sth was true.

Your replacement code indeed misses a colon at the very end. It is syntactically incorrect. If $sth !== true you return $sth. If not, you start a new ternary statement (that is not complete) which I cannot properly fathom. It now does not look at all like the original statement.

The ternary operator is nice and useful, but it's not a block of commands. It is a decision on a value. Rule of thumb, if you can assign something as value to a variable you can use it within the ternary operator :

$var = 3;         // valid
($condition?3:0); // valid

 $var = return 3; //Invalid
 ($condition?return 3:null) //Invalid

You need to:

return ($sth !== true?$sth:null);