开发者

Ternary operator syntax

Do these statements mean the same thing?

(empty($order)) ? $orderBy开发者_运维技巧="ASC" && $order="down" : (($order=="up") ? $orderBy="ASC" && $order="down" : $orderBy="DESC" && $order="up");

if (empty($order)) {
    $orderBy="ASC";
    $order="down";
}
else {
    if ($order=="up") {
        $orderBy="ASC";
        $order="down";
    }
    elseif ($order=="down") {
    $orderBy="DESC";
    $order="up";
    }
}

Or do I have incorrect syntax?


You have the syntax incorrect. The ternary operator is used for assignment on the left for one value only. As far as I'm aware (and would be happy to be proven wrong) you cannot use it to assign two values as you are attempting to do.

// Assign $orderBy via nested ternary:
$orderBy = (empty($order)) ? "ASC" : (($order=="up") ? "ASC" : "DESC");

Actually, this can be a lot more concise and doesn't require nesting:

// Assign $orderBy via nested ternary:
$orderBy = empty($order) || $order == "up" ? "ASC" : "DESC";

// Separately, assign $order
$order = empty($order) || $order == "up" ? "down" : "up";


I think it will help to step throug this statement by statement.

//if empty($order)
(empty($order)) ? 
    // orderBy = ASC && order = down means if( ASC ) order = down
    // since non-empty strings evaluate to true, then empty(order) => orderBy = ASC and order = down
    $orderBy="ASC" && $order="down" : 
    // else (order is not empty) if order = up
    (($order=="up") ? 
        // see above what logic is evaluated here.
        $orderBy="ASC" && $order="down" : 
        // order is not empty and it is not = up
        // orderBy = DESC & order = UP
        $orderBy="DESC" && $order="up");

Because of the nature of all of these different clauses, this entire block returns 'TRUE' to anything in front of empty(order).

It is cleaner and more obvious to have:

if( empty( $order ) || $order == 'up' )
{
    $orderBy='ASC';
    $order='down';
}
else 
/* NOTE: No down referenced in original use 'elseif( $order == 'down' ) */
{
    $orderBy='DESC';
    $order='up';
}

At a minimum, it will bother others less and, honestly, that is a really good policy.

Oh, and BTW, this:

if (empty($order)) {
    $orderBy="ASC";
    $order="down";
}
else {
    if ($order=="up") {
        $orderBy="ASC";
        $order="down";
    }
    elseif ($order=="down") {
    $orderBy="DESC";
    $order="up";
    }
}

Becomes:

(empty($order) || $order == 'up')?$oderBy='ASC' && $order = 'down':($order == 'down')?$orderBy = 'DESC' && $order = 'up': /* replace with your default */ FALSE;

But that will make people ANGRY.


Possibly, but the second version is much clearer. I wouldn't want to maintain code like the first one, even if it does work.


It is called the ternary operator because it takes three operands - a condition, a result for true, and a result for false.

If that sounds like an if statement to you, you are right on the money - the ternary operator is a shorthand (albeit very hard to read) way of doing if statements.

Simplest example :

// Using the ternary operator
<?php
    $agestr = ($age < 16) ? 'child' : 'adult';
?>

// Using the if statement
<?php
   if ($age < 16) {
        $agestr = 'child';
    } else {
        $agestr = 'adult';
    }
?>


I think the best way to judge this is to let the compiler decide. In C#, this is what you get. A compile time error; No, you can't use && operator (or write multiple statements) inside the true part or false part of a ternary operator.

Ternary operator syntax

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜