KevinKuijer
KevinKuijer

Reputation: 35

If else inside echo in php

I want to change this code:

echo"
td class='inhoud_c' width='5%'>".$i."</td>
<td class='inhoud' width='25%'><a href='profile.php?x=".$naam."'>".$naam."</td>
<td class='inhoud_c' width='50%'>
<table border='0' cellspacing='0' style='margin: 0px;'>
    <tr>
   <td>
       <img src='".$icon."' alt='' border='0'>
  </td>
     <td>
         ".$land."
    </td>
</tr>
</table>
</td>
    <td class='inhoud_c' width='20%'>
      ".gmdate("H:i:s", $time)."
    </td>
</tr>   ";
                $i++;

To something like this, but I don't know how to do it:

<td class='inhoud_c' width='20%'>
  "if ($tijz >= 0){
      gmdate("H:i:s", $tijz);
   }
   else {
     echo "Time's Up!";
   }
</td>

So I want a if else statement inside echo, But when i try this the rest of my code doesn't work.

Upvotes: 1

Views: 10804

Answers (3)

rogeriolino
rogeriolino

Reputation: 1135

You can try using ternary operator like this:

( ($tijz >= 0) ? gmdate("H:i:s", $tijz) : "Time's Up!" )

Then:

echo "
td class='inhoud_c' width='5%'>".$i."</td>
<td class='inhoud' width='25%'><a href='profile.php?x=".$naam."'>".$naam."</td>
<td class='inhoud_c' width='50%'>
<table border='0' cellspacing='0' style='margin: 0px;'>
    <tr>
   <td>
       <img src='".$icon."' alt='' border='0'>
  </td>
     <td>
         ".$land."
    </td>
</tr>
</table>
</td>
    <td class='inhoud_c' width='20%'>
      ". ( ($tijz >= 0) ? gmdate("H:i:s", $tijz) : "Time's Up!" ) ."
    </td>
</tr>   ";
                $i++;

Upvotes: 3

Bono
Bono

Reputation: 4849

Is this what you're looking for?

<?php
echo $tijd > 0 ? gmdate('H:i:s', $tijd) : 'Times up!';
?>

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172428

You can use a ternary operator for if else or something like this

<?php
if ($tijz >= 0) {
    echo gmdate("H:i:s", $tijz);
} else {
    echo "Time's Up!";
}
?>

Upvotes: 0

Related Questions