Search code examples
phpgetswitch-statementconditional-statementsdefault-value

Why won't my switch statement generate the correct results?


I am using a condition and a switch to determine some default values for a few variables, but I am not getting the results that I expect.

When the link <a href="index.php?post=myFile"></a> is clicked, I expect to execute the first case in the switch, but the desired variables are not overwritten.

This is my code that are into my index.php:

$titulo = '';
$keywords = '';
$descricao = '';
$post = empty($_GET['post']) ? '' : $_GET['post'];
$pagina = empty($_GET['p']) ? 'home' : $_GET['p'];

if (empty($post)) {
    switch ($pagina):
    case 'posts/myFile':
        $titulo = 'this variable doesnot change the value on this file';
        $keywords = 'this variable doesnot change the value on this file';
        $descricao = 'this variable doesnot change the value on this file';
        break;
    case 'privacidade':
        $titulo = 'Privacidade ';
        break;
    case 'ultimasnoticias':
        $titulo = 'Ultimas Noticias';
        break;
    default:
        $titulo = 'Home';
        $pagina = 'home';
    endswitch;
} else {
    $titulo = 'Post';
}

My current results are:

$_GET["post"] = "myFile";
$titulo = "Post";
$keywords = "";
$descricao = "";
$pagina = "home";

My desired results are:

$_GET["post"] = "myFile";
$titulo = 'this variable doesnot change the value on this file';
$keywords = 'this variable doesnot change the value on this file';
$descricao = 'this variable doesnot change the value on this file';
$pagina = "home";

Why am I not able to update the variables using the first case statement?

Edit:

enter image description here

This is the code that are into my index.php to the nav bar:

<nav>
   <ul>
     <li><a href="?p=home">Início</a></li>
     <li><a href="?p=ultimasnoticias">Últimas Notícias</a>
     <li><a href="?p=contato">Contato</a></li>
  </ul>
</nav>

Solution

  • When you load index.php?post=myFile, you are generating the following element in the $_GET superglobal array:

    $_GET = array ("post" => "myFile");
    

    Then $post = empty($_GET['post']) ? '' : $_GET['post']; declares $post = "myFile".

    This means that if (empty($post)) { evaluates to false (because it is not empty, it is myFile) and the switch-case block is ignored.

    The else condition is executed. $titulo = 'Post';


    Now, if you want the switch-case block to be executed, you must:

    • Change if (empty($post)) { to if (!empty($post)) {.

    As for future trouble you may have within the switch-case block, make sure that you are identically matching the value for $pagina when writing each case. If you aren't sure about what values you are working with or what is being executed, just add echoes in each case to clarify.

    I say this because posts/myFile is not the same string as myFile.


    After more discussion about other hyperlinks, I'll recommend changing the default value for $pagina and extending the if logic to be more inclusive.

    $pagina = empty($_GET['p']) ? '' : $_GET['p'];         // I changed 'home' to ''
    
    if ($post != '' || ($post == '' && $pagina != '')) {   // I changed the logic