简体   繁体   中英

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.

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

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!";
}
?>

Is this what you're looking for?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM