Simple and quick php Querystring

php.pngIf you need to pass one single $var from page to page, this would be the most simple of ways to do it using php. Which also results in very little code for handling it.

First create the url with the var attached. As so.

    http://www.yoursite.com?querystring

Notice there's no assignment using an equal sign as you'd normally do.(Like this)

    http://www.yoursite.com?str=querystring

Now we have to read the query string upon page change.
All you have to write is.

    $var = $_SERVER['QUERY_STRING'];

That's pretty much it. No parsing of the string returned.

You could also use this in other ways, as with if/else blocks and switch/case statements.

    Sample if/else block:
   
    <?php
   
    if ($_SERVER['QUERY_STRING'] == "nhl") {

          doThis();
    }
    elseif ($_SERVER['QUERY_STRING'] == "querystring") {

          doThat();
    }

    ?>


    Sample switch/case block:

    <?php

    switch($_SERVER['QUERY_STRING']) {

       case 'nhl':
          doThis();
          break;

       case 'querystring':
          doThat();
          break;

       default:
          doSomething();

    }

    ?>


Of course as I mentioned at the beginning of this article, this method for query strings is only useful for passing one value, which results in not having to parse the returned string with extra code and processing times.

This way of retrieving a query string can also be applied to Javascript in much the same manner, as I wrote about previously and can be read about here.


Leave a comment

BMW, Toyota, BMW, Saab, Audi, Nissan Mercedes, BMW, Nissan, Honda,bmw bmw bmw Acura, Honda, Acura Forerunner, SUV, BMW, Mercedes, Toyota  
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan, Honda,Volvo Volvo Volvo Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan, Honda,Volvo Volvo Volvo Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota


Chicago Blackhawks
Chicago Blackhawks
Stanley Cup
Stanley Cup
Pittsburgh Penguins
Pittsburgh Penguins
Boston Bruins
Boston Bruins
Fire & Flames
Fire & Flames








 


Categories

BMW, Toyota, BMW, Saab, Audi, Nissan Mercedes, BMW, Nissan, Honda, Acura, Honda, Acura Forerunner,bmw bmw bmw SUV, BMW, Mercedes, Toyota