Change background color of a page using php

I have a standard html page so:

and so on

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

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?

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

Thanks for any help in advance

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

put your <body> tag inside the if else

if blablabla {

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

Solution 2

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

if ( your_conditional ) {
  $styleBlock = sprintf('
    <style type="text/css">
       body {
  ', $yourColor);
  echo $styleBlock;

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

One caution: if you have additional style rules for the body tag’s background color farther down your page, those will take precedence over the one from your if statement.

Solution 3

here is code that i whant to change background of body

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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply