如果文本的颜色等于 PHP 中的给定值,如何更改文本的颜色

我是 PHP 新手,试图从数据库获取数据并检查该数据是否等于某个值。如果是这种情况,我想将文本颜色更改为红蓝色或绿色,我尝试了以下代码,但如果我运行它,我的页面会崩溃,如果我删除它,我会得到我想要的所有数据。有人可以帮我吗,我对 PHP 很陌生。


这是我的数组


$arrayValues[] = array($Names, $Title, $Danger);

这是我的代码


<?php

if($Danger == "High")

{

   <font color="red">$Danger</font>

}


if($Danger == "Medium")

{

   <font color="Green">$Danger</font>

}   

?>


素胚勾勒不出你
浏览 79回答 3
3回答

慕盖茨4494581

您需要使用echoPHP 标签来显示 HTML。fontHTML5 不支持该标记。您最好p使用 CSS 来使用和更改其颜色。从您的问题来看,您似乎正在尝试循环遍历数组。可以这样做:<?phpforeach($arrayValues as $arrayValue) {&nbsp; &nbsp;$Danger = $arrayValue[2];&nbsp; &nbsp;if($Danger == "High") {&nbsp; &nbsp; &nbsp; echo'<p style="color: red;">'.$Danger.'</p>';&nbsp; &nbsp;}else if($Danger == "Medium") {&nbsp; &nbsp; &nbsp; echo'<p style="color: green;">'.$Danger.'</p>';&nbsp; &nbsp;}}?>

青春有我

您可以使用三元运算符,例如:// this equal to: if () { true } else { if () { true } else { false } }//                         ^                     ^              ^//                        red                   green         black (default color) $color = $Danger == "High" ? 'red' : ($Danger == "Medium" ? 'green' : 'black');echo "<font color='$color'>$Danger</font>";

小怪兽爱吃肉

您必须使用 echo 命令将 html 作为字符串输出,如下所示:<?phpif($Danger == "High"){echo "<font style=\"color:red\">" +&nbsp; $Danger + "</font>";}...?>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5