Change the background color of a page using PHP

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

Notice: Undefined variable: body in C:\xampp\htdocs\Test\inc\header.inc.php on line 11

Index.php

if(isset($body) || $body == true)
{
    echo '<body style="background-color:white">';
} else {
    echo '<body style="background-color:orange">';
}

?>
 6
Author: Ivan Botero, 2016-09-26

2 answers

The problem as says in his comment, is that the variable $body (which you use in the condition of the if) is undefined. And the grace is that one of the checks is to see if it is defined or not.

The problem is that you are connected with an O (||), when you should be using a Y (&&). When you use ||, all conditions are checked in order until one is true (and the rest are ignored); when you use a &&, all conditions are checked conditions in order until one is false (and the rest is ignored). You are checking that $body is defined: it is not, so it is false, but since you use ||, it is passed to the following condition $body == true and that is the one that throws you the error because you are trying to access an Undefined variable.

The solution would be simple, use a Y (&&):

if(isset($body) && $body == true)
{
    echo '<body style="background-color:white">';
} else {
    echo '<body style="background-color:orange">';
}
 8
Author: Alvaro Montoro, 2017-04-13 13:00:52

@AlvaroMontoro, see for example

if(isset($body) && $body == true)
{
    echo '<body style="background-color:white">'; --> login.php cuando si acepta "Entrar" index.php abajo color orange
} else {
    echo '<body style="background-color:orange">'; --> "Entrar" al sitio index.php
}
 0
Author: Diego Sagredo, 2016-10-22 03:13:56