Skip to content
Advertisement

How to style something based on the URL?

I have a wrapper that is displaying on the whole screen and I want it so if I enter ?display=0 into my URL that the wrapper will disappear with PHP or JavaScript. I’ve been searching for 2 hours and these are the things I found:

$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == 'http://example.com/home?display=0') { ... }
if(location.hash == "") { ... }
if($_GET['display' == 1]) { ... }

But none of them worked so is there any way to do this in PHP or JavaScript?

Advertisement

Answer

You were close in what you were doing.

if($_GET['display' == 1]) { ... }
                 ^     ^

You have a rather serious typo, you misplaced the closing bracket of the $_GET array.

Changing the above to this should yield some results;

if($_GET['display'] == 1 ) { ... }

Although I personally would do a check to see if “display” is set at all, so you’d end up with something like;

if ( isset( $_GET['display'] ) ) {
    // The URL included a ?display= parameter
    if ( $_GET['display'] == 1 ) { ... }

}else{
    // Default behaviour if there is no ?display= in the URL
}

If you don’t do a check like this, PHP will throw an “Undefined index: display” error if someone opens the page without the ?display= bit added to the URL.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement