PHP:根据 $value 更改文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2804188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:43:11  来源:igfitidea点击:

PHP: Change color of text based on $value

phpcolors

提问by Kevin Brown

What's the easiest way to change a text's color based on a variable?

根据变量更改文本颜色的最简单方法是什么?

For example: If $var is between 1-5, green. Between 6-10, Orange. Greater than 11, Red.

例如:如果 $var 介于 1-5 之间,则为绿色。6-10 之间,橙色。大于 11,红色。

回答by sshow

function getProperColor($number)
{
    if ($var > 0 && $var <= 5)
        return '#00FF00';
    else if ($var >= 6 && $var <= 10)
        return = '#FF8000';
    else if ($var >= 11)
        return = '#FF0000';
}

And use it like this

并像这样使用它

<div style="background-color: <?=getProperColor($result['number'])?>;"><?=$result["title"]?></div>

回答by LukeN

$color = "#000000";

if (($v >= 1) && ($v <= 5))
   $color = "#00FF00";
else if (($v >= 6) && ($v <= 10))
   $color = "#FF9900";
else if ($v >= 11)
   $color = "#FF0000";

echo "<span style=\"color: $color\">Text</span>";

回答by doc

Are color values indexed by constants? I would prepare hash map

颜色值是否由常量索引?我会准备哈希图

$colorMap[0] = '#00FF00'; //green
$colorMap[1] = '#0000FF'; //blue
$colorMap[2] = '#FF0000'; //red
$colorMap[3] = '#330000'; //dark red

and so on. Then use CSS

等等。然后使用CSS

<span style="color: <?php echo $colorMap[$var]; ?>;">desired color</span>

回答by zaf

I'll use CSS colors and also highlight the fact that the number 11 does not map to any color according to your rules making most of the answers invalid :)

我将使用 CSS 颜色并强调数字 11 不会根据您的规则映射到任何颜色的事实,这使得大多数答案无效:)

<?php

$color=getColor(11);

function getColor($n){

    // Is number between 1 and 5?
    if($n>=1 && $n<=5) return "green";

    // Is number between 6 and 10?
    if($n>=6 && $n<=10) return "orange";

    // Is number greater than 11
    if($n>11) return "red";

    // Return default (black) for all other numbers
    return "black";

}

?>

<span style='color:<?=$color?>'>Text</span>

回答by fafnirbcrow

You need to actually use elseif statements if your going to use a set of if statements,

如果要使用一组 if 语句,则需要实际使用 elseif 语句,

 if ($var < 6) $color = '#00FF00';
elseif ($var < 10) $color = '#FF8000';
elseif ($var > 10) $color = '#FF0000';

回答by JYelton

Something like this trio of if statements:

类似于这三个 if 语句:

if ($var < 10) $color = '#FF8000';
if ($var < 6) $color = '#00FF00';
if ($var >= 10) $color = '#FF0000';

echo "<span style=\"color: $color;\">This text is colored.</span>";

回答by Rik Heywood

A simple solution might be to do something like this...

一个简单的解决方案可能是做这样的事情......

if ($var < 6)
    $style="0F0";
else if ($var < 11)
    $style="F50";
else
   $style = "F00";

?><div style="color:#<?php echo $style; ?>">blar</div>

回答by alexganose

Ternary operator for your simple example.

简单示例的三元运算符。

  $color = ($var < 6) ? '#FF8000' :  (($var < 10) ? '#00FF00' : '#FF0000');

回答by Finzzownt

Assuming one works with ranges than this is quite flexible:

假设一个工作范围比这非常灵活:

function getProperColor($range, $value)
{
    foreach($range as $key => $color)
    {
        if ($value <= $key)
            return $color;
    }
    return $color;
}

$colorRange = array(
    5 => 'green',
    10 => 'orange',
    11 => 'red'
);

for($i=-1;$i<16;$i+=2)
{
    echo "$i:" . getProperColor($colorRange, $i) . "\n";
}

This will output:

这将输出:

-1:green
1:green
3:green
5:green
7:orange
9:orange
11:red
13:red
15:red