php 使用php更改页面的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5475422/
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 21:33:45  来源:igfitidea点击:

Change background color of a page using php

php

提问by RSM

I have a standard html page so:

我有一个标准的 html 页面,所以:

<html>
<head>..
.
..
and so on

and i have a php script linked to that html page with:

我有一个链接到该 html 页面的 php 脚本:

if blablabla {

change background color;
}

does anyone know how to change the background color of my page if the conditions in the if statement are met?

如果满足 if 语句中的条件,有人知道如何更改页面的背景颜色吗?

I hope i made this clear, if not, please say which bit is unclear.

我希望我说清楚了,如果没有,请说哪一点不清楚。

Thanks for any help in advance

感谢您提前提供任何帮助

回答by Shakti Singh

put your <body>tag inside the if else

把你的<body>标签放在 if else 里面

if blablabla {

 echo '<body style="background-color:white">';
}
else {
 echo '<body style="background-color:orange">';
}

回答by Kyle Humfeld

It's not necessarily great practice, but it should get the job done:

这不一定是很好的做法,但它应该可以完成工作:

if ( your_conditional ) {
  $styleBlock = sprintf('
    <style type="text/css">
       body {
         background-color:%s
       }
    </style>
  ', $yourColor);
  echo $styleBlock;
}

One good (?) thing about this is that with this technique, you can put your ifstatement anywhere - it'll act on the body tag regardless of where you put it.

关于这一点的一个好处(?)是,使用这种技术,您可以将您的if语句放在任何地方 - 无论您将它放在何处,它都会对 body 标签起作用。

One caution: if you have additional style rules for the bodytag's background color farther down your page, those will take precedence over the one from your ifstatement.

一个警告:如果您对body页面下方的标签背景颜色有其他样式规则,则这些规则将优先于您的if语句中的规则。

回答by betrice mpalanzi

here is code that i whant to change background of body

这是我想改变身体背景的代码

<body bgcolor="<?php echo $name2; ?>">