Condition to appear link does not work

0

I have the following logic that depending on what the user type, will return a link, however it does not work.

<nav id="menu">
<ul>
    <li><a href="index.php">Minimo Nulo</a></li>
    <li><a href="Fechamento.php">Divergência de campos </a></li>

    <?php

    if($nivel != "MM" || $nivel != "AAAA"){

      $function = '';

    } else {

    $function = '<li>''<a href="Alt_Papel.php">' . Alterar papel - D . '</a>''</li>';

    }

    echo $function;

    ?>
</ul>
</nav>
    
asked by anonymous 20.12.2017 / 15:53

2 answers

1

First, I would recommend using the identical operator in PHP to compare strings that would be !== instead of just != or else use strcmp() to do so.

Another thing to say is that the second assignment to the string value of the $function variable is in trouble, you should not concatenate the string this way.

One more addition would be to use an if ternary operator to accomplish such a condition imposed by you, without the need to create a variable as well.

In this part you should use . to delimit a string from another, or make the string as one.

$function = '<li>''<a href="Alt_Papel.php">'.Alterar papel - D.'</a>''</li>'; //isto é totalmente incorreto.
$function = '<li>'.'<a href="Alt_Papel.php">'.'Alterar papel - D'.'</a>'.'</li>'; //isto é correto, porém não necessário.
$function = '<li><a href="Alt_Papel.php">Alterar papel - D</a></li>'; //isto é ideal.

For example, as there is another incorrect part in your string as well.

Applying the recommendations I mentioned your code would look like this:

<nav id="menu">
<ul>
    <li><a href="index.php">Minimo Nulo</a></li>
    <li><a href="Fechamento.php">Divergência de campos </a></li>

    <?php
    echo ($nivel !== 'MM' || $nivel !== 'AAAA') ? '' : '<li><a href="Alt_Papel.php"> Alterar papel - D </a></li>';
    ?>
</ul>
</nav>
    
20.12.2017 / 16:58
1

You are concatenating where you do not need to concatenate, the right thing would be ...

$function = '<li><a href="Alt_Papel.php"> Alterar papel - D </a></li>';
    
20.12.2017 / 16:10