Andrew
Andrew

Reputation: 123

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?

Upvotes: 1

Views: 2157

Answers (5)

cwallenpoole
cwallenpoole

Reputation: 82028

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.

Upvotes: 2

Saeed Neamati
Saeed Neamati

Reputation: 35822

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. enter image description here

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270627

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";

Upvotes: 3

Saurabh Gokhale
Saurabh Gokhale

Reputation: 46405

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';
    }
?>

Upvotes: 1

Rob Agar
Rob Agar

Reputation: 12459

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

Upvotes: 2

Related Questions