-4

I want to embed following if else condition into php string.

Here is Code:

<?php
    $selected = get_result();   //return value can be TRUE or FALSE
    if($selected)
        echo "<tr align='center' class='selected_row'>";
    else
        echo "<tr align='center' class='red_bg'";
?>
Rashid
  • 1,244
  • 3
  • 13
  • 29
  • 1
    A conversion to ternary is not going to save you much. If this code is used many time convert it to a function that you can call. – Jay Blanchard Feb 13 '15 at 19:32
  • possible duplicate of [How to write a PHP ternary operator](http://stackoverflow.com/questions/17981723/how-to-write-a-php-ternary-operator) – Rizier123 Feb 13 '15 at 19:32

3 Answers3

2

The ternary (or conditional) operator should do the trick. Something like this:

echo "<tr align='center' class='" . ($selected ? "selected_row" : "red_bg") . "'>";

It's a matter of personal preference whether this is more readable than the original version or not. Keep in mind that code readability and supportability is vastly more important than the "number of bytes" used by the code file.

If this is really happening a lot in the code then there's a fairly decent chance that you could make the code far more clean and expressive by using some kind of templating system to bind page markup to a model or by otherwise refactoring the page rendering into more generic and re-usable modules.

David
  • 208,112
  • 36
  • 198
  • 279
2

A conversion to ternary is not going to save you much. If this code is used many time convert it to a function that you can call.

function setSelectedClass($selected) { 
    if(true == $selected) {
        $row = "<tr align='center' class='selected_row'>";
    } else {
        echo "<tr align='center' class='red_bg'">;
    }
    return $row
}

Now you don't have to have even ternary statements sprinkled all over and if you want you can use a ternary condition in the function like this:

function setSelectedClass($selected) { 
    $row = "<tr align='center' class='" . (true == $selected ? "selected_row" : "red_bg") . "'>";
    return $row;
}

Once you have the function in place you just call it:

setSelectedClass(true);

This will save you a lot of code. Anytime you are repeating code, as you are finding that you do, consider creating a function.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
0

Like David said, ternary operator should be the best here.

From http://php.net/manual/en/language.operators.comparison.php :

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

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.

If you don't like this one, you still can do this in three echos (but that won't solve your bytes problem) :

<?php
echo "<tr align='center' class='";
if($selected)
    echo 'selected_row';
else
    echo 'red_bg';
echo "'>";
?>

Or with a variable :

<?php
if($selected)
    $class = 'selected_row';
else
    $class = 'red_bg';

echo "<tr align='center' class='$class'>";
?>

But ternary operator stays the cleanest to me.

Niols
  • 627
  • 1
  • 4
  • 13