我尝试根据两个值显示不同的颜色。
首先,如果值相等,则显示绿色,效果很好
<?php if (miletofurlong($pasthorse['distance']) == $todaysdistance)
{
$Horsedist = "<b><span style='color:#ff4500 '>". miletofurlong($pasthorse['distance'])."f</span></b>";
} ?>
现在我想做的是如果 miletofurlong($pasthorse['distance'])
不等于但仍介于 ($todaysdistance + 1) 和 ($todaysdistance + 1) 之间)然后将其突出显示为蓝色。
我运行了以下代码,但它似乎突出显示了所有内容,我不确定为什么
if (miletofurlong($pasthorse['distance']) == $todaysdistance)
{
$Horsedist = "<b><span style='color:#ff4500 '>". miletofurlong($pasthorse['distance'])."f</span></b>";
}
elseif (miletofurlong($pasthorse['distance']) !== $todaysdistance and miletofurlong($pasthorse['distance']) <= ($todaysdistance + 1) or miletofurlong($pasthorse['distance']) !== $todaysdistance and miletofurlong($pasthorse['distance']) >= ($todaysdistance - 1) )
{
$Horsedist = "<b><span style='color:blue'>". miletofurlong($pasthorse['distance'])."f</span></b>". ($todaysdistance + 1). "and" . ($todaysdistance - 1);
}
else
{
$Horsedist = miletofurlong($pasthorse['distance'])."f" ;
}
最佳答案
您缺少括号,并且您的 if 情况有点困惑。让我们首先使用一个变量并在其中声明您的 $pasthorse。
$distance = miletofurlong($ pasthorse['distance']);
if ($distance == $todaysdistance)
$Horsedist = "<b><span style='color:#ff4500 '>". $distance ."f</span></b>";
else if ($distance <= ($todaysdistance + 1) and $distance >= ($todaysdistance - 1) )
$Horsedist = "<b><span style='color:blue'>". $distance . "f</span></b>" ($todaysdistance + 1). "and" . ($todaysdistance - 1);
else
$Horsedist = $distance ."f" ;
这样,它应该工作得更好,并且更具可读性。
关于php - 值(value)比较未按预期进行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30325048/