php if does not work when you have nothing declared by url

Asked

Viewed 41 times

0

I have this error in the variable put to show the code only when you have the reference. but the error when you have nothing declared.

That is, when accessing the site with site.com/? ref=app (right), but when access without, it gives the error below.

Error when nothing is set in URL:

Warning: Undefined array key "ref" in \header.php on line 1

My current code name:

<?php if ($_GET["ref"] == "app") : ?>
  <style media="screen">
    #sidebar {
      display: none !important;
    }

    .navbar {
      display: none !important;
    }
  </style>
<?php endif; ?>

2 answers

0

Oops friend. I believe we’re missing the else. has tested?

(condition ? action_if_true: action_if_false;)

From what I understand of your code, he’s checking to see if you see the "($_GET["ref"] == "app")". In the case when you put it right in the url it will, but if you do not put, it is falling in the else which causes nothing to happen.

0

Dude, I’m trying to learn PHP. I’m running the risk of being wrong. But I think your problem can be solved with isset.

https://www.php.net/manual/en/function.isset

I think maybe I don’t even need to use the if Else because the isset behaves as such. Using isset you can check whether the parameter sent by the url has been defined. So it would look something like this:

<?php 
        $ref = isset($_GET["ref"])?$_GET["ref"]:0;
        print $ref; // para testar o parametro recebido da url
        #seu codigo...
    ?>

Browser other questions tagged

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