Php if,else if echo语句

Php if,else if echo语句,php,mysql,if-statement,Php,Mysql,If Statement,如果该值为1,则单元格bg为绿色,单元格中为#1 如果该值为0,则单元格bg为黄色,其中包含一个0 我想显示“是”而不是“1”,显示“否”而不是“0” if($row['play']==1){ 回显“$row['play']”; } else if($row['play']==0){ 回显“$row['play']”; } 这些值来自复选框(1)和隐藏字段(0)。MySQL字段是BOOL 有没有更简单/更好的方法来实现这一点 您可以这样做: if($r

如果该值为1,则单元格bg为绿色,单元格中为#1 如果该值为0,则单元格bg为黄色,其中包含一个0

我想显示“是”而不是“1”,显示“否”而不是“0”

if($row['play']==1){
回显“$row['play']”;
}
else if($row['play']==0){
回显“$row['play']”;
}                    
这些值来自复选框(1)和隐藏字段(0)。MySQL字段是BOOL

有没有更简单/更好的方法来实现这一点

您可以这样做:

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

您可以将其强制转换为int,如果大于零,则生成一个条件:

if((int)$row['play'] > 0) {
    echo "<td bgcolor='green'>Yes</td>";
}
else {
    echo "<td bgcolor='yellow'>No</td>";
}
if((int)$row['play']>0){
回应“是”;
}
否则{
回应“否”;
}
这样,
play
可以是1、2、3、4、5……等等。

$words=array(
$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>";
} 
0=>“否”, 1=>“是” ) ; 如果($row['play']==1){ 回显“$words[(int)$row['play']]”; }else if($row['play']==0){ 回显“$words[(int)$row['play']]”; }
或者更好:

$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>";
$map=array(
0=>数组(“单词”=>“否”,“颜色”=>“黄色”),
0=>数组(“单词”=>“是”,“颜色”=>“绿色”),
) ;
$current=(int)$row['play'];
回显“{$map[$current]['word']}”;
试试这个

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

像这样的东西就可以了-

echo "<td bgcolor='" . $row['play'] == 1 ? "green" : "yellow" . "'> . $row['play'] == 1 ? "No" : "Yes" . "</td>";
echo“$row['play']==1?”否:“是”;

您可以通过执行以下操作来减少echo语句:

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>";
if($row['play']==1){
$color='绿色';
$text='yes';
}
其他的
{
$color='red';
$text='no';
}
//根据输入指定颜色和文本值。
回显“$data”;

if($row['play']==1){echo“Yes”}否则if($row['play']==0){echo“No”}
bgcolor
属性已被弃用,请改用CSS。顺便说一句,为什么这样的问题会被选中!如果你这样做,你最好在数组中保留
绿色
黄色
,然后删除If。当然,我也回答了这个问题。
echo "<td bgcolor='" . $row['play'] == 1 ? "green" : "yellow" . "'> . $row['play'] == 1 ? "No" : "Yes" . "</td>";
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>";