Jump to content
Larry Ullman's Book Forums

Recommended Posts

In chapter 10, in pagination, I am having trouble understanding why the background colors of the table will alternate based on the ternary operator used. I don't understand why $bg will change from #eeeeee to #ffffff. Before the ternary operator $bg is set to #eeeeee, and I don't see how that will change based on the code. Here is the code.

 

$q = "SELECT last_name, first_name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr, user_id FROM users ORDER BY registration_date ASC LIMIT $start, $display";
$r = @mysqli_query ($dbc, $q);
echo '<table align="center" cellspacing="0" cellpadding="5" width="75%"><tr><td align"left"><b>Edit</b></td><td align="left"><b>Delete</b></td>';
echo '<td align="left"><b>Last Name</b></td><td align="left"><b>First Name</b></td><td align="left"><b>Date Registered</b></td></tr>';
$bg = '#eeeeee';
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
echo '<tr bgcolor="' . $bg .'"><td align="left"><a href="edit_user.php?id=' . $row['user_id'] . '">Edit</a></td><td align="left"><a href="delete_user.php?id=' . $row['user_id'] . '">';
echo 'Delete</a></td><td align="left">' . $row['last_name'] . '</td><td align="left">' . $row['first_name'] . '</td><td align="left">' . $row['dr'] . '</td></tr>';
}
echo '</table>';
Link to comment
Share on other sites

Hello, and welcome to the forums.

 

The code initializes $bg to '#eeeeee', but what the ternary operator does is say, "If $bg is equal to '#eeeeee', then make it equal to '#ffffff'. And if it's not equal to '#eeeeee', then make it equal to '#eeeeee'."

 

That make sense?

 

In essence, it's like the following:

$bg = '#eeeeee';

while (something) {
  if ($bg == '#eeeeee') {
    $bg = '#ffffff';
  } else {
    $bg = '#eeeeee';
  }
}
  • Upvote 1
Link to comment
Share on other sites

  • 3 weeks later...

I also love ternary. My "rule" is that I replace a ternary with a normal IF-statement when the logic is pushed to far right. Legibility is often the most important for me, so I keep that in mind. I also love the "replace logic with query" refactoring, and use that often for legibility both in if-statements and when using the ternary operator:

public function something( $value )
{
   if ( self::SOME_MINIMUM_CONSTANT < $value && $value < self::SOME_MAXIMUM_CONSTANT )
   {
      return self::SOME_CONTANT_VALUE;
   }

   return self::OTHER_CONSTANT_VALUE
}

// Too long Ternary
public function something( $value )
{
   return ( self::SOME_MINIMUM_CONSTANT < $value && $value < self::SOME_MAXIMUM_CONSTANT ) ? self::CONSTANT : self::OTHER_CONSTANT;
}

// Refactoring

public function something( $value )
{
   return ( $this->expressiveMethod($value) ? self::CONSTANT_VALUE : self::OTHER_CONSTANT;
}

private function expressiveMethod( $value )
{
   return self::SOME_MINIMUM_CONSTANT < $value && $value < self::SOME_MAXIMUM_CONSTANT )
}
  • Upvote 1
Link to comment
Share on other sites

You can nest ternary operators, but it can be hard to read, and is generally not recommended since you have so many easier-to-read options.

 

I thought it wasn't possible now i have learned more than one new thing today. Sometimes i think its better when its easier to read the code, imagine someone else comes to update your code, having nested tenary operators will make it more confusing for for them to interpret and they might spend 10 times longer to do the job.

 

Have you every looked at the Drupal code its like kindergarten to what we are doing now. Yii kicks its as big time...

Link to comment
Share on other sites

  • 2 weeks later...

I came to think about this thread once I finished writing the following code. Haha! :D

$productId = isset($_POST['sku']) ? $_POST['sku'] : isset($_GET['sku']) ? $_GET['sku'] : false;

What do you guys think? Is nesting ternary operators ALWAYS bad or would you let something like that slide? To make a case for the line, an if/else statement would take way more space, and take focus away from the more important code.

 

It will be interesting to see what you guys think.

Link to comment
Share on other sites

I would avoid nested ternary operators myself. It's too hard to read, and I place a value on creating readable code. I also don't buy into the "shorter is better" theory of programming. I'd like to be able to look at a line or block of code and understand what it's doing relatively quickly. 

Link to comment
Share on other sites

 Share

×
×
  • Create New...