Display a Query String Value on a Web Page

It seems simple, almost basic, but many people want to display the contents of a query string on a page for the user or just for troubleshooting purposes. Everyone does it, everyone needs to do it, and there are a few different options I’m going to introduce below. First, our sample URL:



http://www.phpreferencebook.com/?variable=value&name=Mario Lurig&gender=male!
  1. Display the whole string (everything after the question mark)
    • <?php echo $_SERVER['QUERY_STRING']; ?>
      • variable=value&name=Mario%20Lurig&gender=male
  2. Display the whole string decoded (convert %## to original characters)
    • <?php echo urldecode($_SERVER['QUERY_STRING']); ?>
      • variable=value&name=Mario Lurig&gender=male!
  3. Show each variable and value as an array (already decoded)
    • <?php print_r($_GET); ?>
      • Array ( [variable] => value [name] => Mario Lurig [gender] => male!
    • <?php echo '<pre>'; print_r($_GET); echo '< /pre>'; ?>
      • Array
            [variable] => value
            [name] => Mario Lurig
            [gender] => male!

There are lots of options from there, so don’t let this be the end of your exploration of how to display a query string value on a page. If you have any great tricks, feel free to share them in the comments below!

$_SERVER[‘QUERY_STRING’] in Global Variables

(pg. 33)

The description in the book reads:

$_SERVER[‘QUERY_STRING’] – The current scripts path

This description is the same as the entry above, because I made a bad edit (copy/paste for formatting). The correct description would be as follows:

$_SERVER[‘QUERY_STRING’] – The current query string (without the question mark)

The example is correct and accurate, and luckily this is painfully obvious thanks to the name of the key QUERY_STRING in the $_SERVER array.