Condition to appear link does not work

Asked

Viewed 67 times

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>

2 answers

1

You’re concatenating where you don’t need to concatenate...

$function = '<li><a href="Alt_Papel.php"> Alterar papel - D </a></li>';
  • This post is falling into "low quality" due to the size of your content. Please make an example of your answer. Review

1


First, I would recommend using the identical operator in PHP to compare strings which in this case would be the !== rather than just != or use the strcmp() to that end.

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

Another addendum would be to use an if ternary operator to realize such a condition imposed by you without the need to create a variable as well.

In this part you should use a . to delimit a string from another, or to 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 only, because there is also another incorrect part in your string.

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

<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>
  • 1

    Thanks man, I’m starting with PHP I’m giving some vaccinated on some things I don’t understand much yet, but thank you so much for the tips and the answer

  • Good luck in this journey, PHP is somewhat rustic and problematic I would say hehe

  • 1
  • 1
  • @Pauloroberto Yes I noticed kkk, most of the solutions I had found the problems I had were "gambiarras".

  • But keep in mind. Gambiarra is a matter of point of view, most of the time there will always be a better way to do something that you are doing. Just try to limit yourself to what language has defined as limits, and not just to limit yourself to what your knowledge allows.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.