If, else if echo statement

If the value is 1, cell bg is green with No. 1 in the cell. If the value is 0, cell bg is yellow with 0 in it.

I would like to display "Yes" instead of "1" and "No" instead of "0"

if($row['play'] == 1){ echo "<td bgcolor='green'>" . $row['play'] . "</td>"; } else if ($row['play'] == 0){ echo "<td bgcolor='yellow'>" . $row['play'] . "</td>"; } 

The values ​​are taken from the flag (1) and the hidden field (0) The MySQL field is BOOL.

Is there an easier / better way to achieve this?

+4
source share
6 answers

You can do it:

  if($row['play'] == 1){ echo "<td bgcolor='green'>Yes</td>"; } else if ($row['play'] == 0){ echo "<td bgcolor='yellow'>No</td>"; } 

but I would say that switch/case thing is more convenient:

 switch( $row['play'] ) { case 1: echo "<td bgcolor='green'>Yes</td>"; break; default: echo "<td bgcolor='yellow'>No</td>"; break; } 
+6
source

You can pass it to int and then fulfill the condition if it is above zero:

 if((int)$row['play'] > 0) { echo "<td bgcolor='green'>Yes</td>"; } else { echo "<td bgcolor='yellow'>No</td>"; } 

So play can be 1, 2, 3, 4, 5, .. cc.

0
source
 $words = array( 0 => "No", 1 => "Yes" ) ; if($row['play'] == 1){ echo "<td bgcolor='green'>" . $words[(int)$row['play']] . "</td>"; } else if ($row['play'] == 0){ echo "<td bgcolor='yellow'>" . $words[(int)$row['play']] . "</td>"; } 

Or even better:

 $map = array( 0 => array("word"=>"No", "color"=>"yellow"), 0 => array("word"=>"Yes", "color"=>"green"), ) ; $current = (int) $row['play'] ; echo "<td bgcolor='{$map[$current]['color']}'>{$map[$current]['word']}</td>"; 
0
source

try it

 if($row['play'] == 1) { echo "<td bgcolor='green'>Yes</td>"; } else if ($row['play'] == 0) { echo "<td bgcolor='yellow'>No</td>"; } 
0
source

something like this will do it -

 echo "<td bgcolor='" . $row['play'] == 1 ? "green" : "yellow" . "'> . $row['play'] == 1 ? "No" : "Yes" . "</td>"; 
0
source

You can cut the echo instruction by doing:

 if($row['play'] == 1){ $color = 'green'; $text = 'yes'; } else { $color = 'red'; $text = 'no'; } // Assign the color and text values based on the input. echo "<td bgcolor=$color>$data</td>"; 
0
source

All Articles