php 如何在echo中更改PHP中变量的字体颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14574842/
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
How can i change the font color of a variable in PHP inside echo
提问by Kristine Yamasaki
echo $display;
so we know the default color is black. But i want to change it to white. Tried codes such as
所以我们知道默认颜色是黑色。但我想把它改成白色。尝试过的代码,例如
"<div style=\"color: white;\">$display</div>";
but it doesn't work. Can you help me with this guys? thanks in advance.
但它不起作用。你能帮我处理这些家伙吗?提前致谢。
回答by Matt Clark
<?php
$Color = "red";
$Text = "This text is red";
echo '<div style="Color:'.$Color.'">'.$Text.'</div>';
?>
In php, you can use .to add variables together. In this case, the final output of the echo line is:
在php中,可以使用.将变量加在一起。在这种情况下,回声线的最终输出是:
<div style="Color:red">This text is red</div>
An example of this code in use can be seen here.
可以在此处查看正在使用的此代码的示例。
回答by K-Gun
If applied color doesn't work even inline-style, so you can use !importantword, cos maybe all div colors come from css file and suppress inline-styles.
如果应用的颜色甚至内联样式都不起作用,那么您可以使用!importantword,cos 可能所有 div 颜色都来自 css 文件并抑制内联样式。
"<div style=\"color: white !important;\">$display</div>";
回答by Pir Fahim Shah
If you have a div and you want to change its text and color of text, then simply copy and paste this code it will work for you.
如果您有一个 div 并且想要更改其文本和文本颜色,那么只需复制并粘贴此代码即可。
echo "<script type='text/javascript'>";
echo"var ele= document.getElementById('login_status');
ele.innerHTML ='* Your <font color=#ff3322> Email </font> is incorrect ';";
echo "</script>";
回答by Devesh Kaushik
<span style="color:white;"><?php echo $display; ?></span>
Hope it might help.
希望它可能会有所帮助。
回答by Alex Doukas
I know this is an old question but I write the answer in case someone has the same question and does not know where to look. I think you wanted to print links so this is why the previous methods didn't work.Inside your php file write the following:
我知道这是一个老问题,但我会写下答案,以防有人有同样的问题并且不知道去哪里找。我想你想打印链接,所以这就是之前的方法不起作用的原因。在你的 php 文件中写下以下内容:
echo "<html>
<head>
<style>
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* mouse over link */
a:hover {
color: hotpink;
}
/* selected link */
a:active {
color: blue;
}
</style>
</head>
<body>
$variableYouWantToChangeColor
</body>
</html>";
You can change/remove parts of the css so it fits your needs.
您可以更改/删除部分 css 以使其适合您的需要。
回答by reza lendra
its usable with bootstrap
它可以与引导程序一起使用
<p class="text-primary"><?php echo $distance; ?></p>
i found that when i try echo result from a calculation
我发现当我尝试从计算中回显结果时
<p class="text-primary"><?php echo round ($distance,2); ?></p>
回答by e-Learner
<?php
$yourtext = "Some text";
?>
<div style="color:<?php echo ((@$_GET["color"]) && !empty(@$_GET["color"])) ? $_GET["color"] : "black"; ?>" ><?php echo $yourtext; ?></div>
In Url http://yoursite.you/yourpage.php?color=white
在网址中 http://yoursite.you/yourpage.php?color=white
Green
绿
Yellow
黄色
Blue etc...
蓝色等...

