Use the AND and OR operators on the same conditional

0

In the following example the check does not work if the page was sobre , in which case it displays the servicos page even though it is not required in the validation. Whats wrong? I realized that this problem only happens in elseif

$status = 1;
$pagina = "sobre";

if($pagina = "home" AND $status == 2 OR $status == 3) {

    echo "Página Home";

}elseif($pagina = "servicos" AND $status == 1 OR $status == 2) {

    echo "Página Serviços";

}elseif($pagina = "sobre" AND $status == 1 OR $status == 2) {

    echo "Página Serviços";

}
    
asked by anonymous 06.11.2018 / 09:18

1 answer

2
  

in this case it displays the services page

Note that in $pagina = "home" you are assigning the string home to the variable $pagina !

For comparisons, the operator is == (Compare value) or === (Compare type and value).

Your condition should be changed too:

if($pagina == "home" AND ($status == 2 OR $status == 3)) { // ...

In conclusion, your code should look like this:

$status = 1;
$pagina = "sobre";

if($pagina == "home" AND ($status == 2 OR $status == 3)) {  // PAGINA DEVE SER "home" E STATUS DEVE SER 2 OU 3

    echo "Página Home";

} else if($pagina == "servicos" AND ($status == 1 OR $status == 2)) { // PAGINA DEVE SER "servicos" E STATUS DEVE SER 1 OU 2

    echo "Página Serviços";

} else if($pagina == "sobre" AND ($status == 1 OR $status == 2)) { // PAGINA DEVE SER "home" E STATUS DEVE SER 1 OU 2

    echo "Página Serviços"; // Página Sobre!!!

}
    
06.11.2018 / 10:01