PHP Reference Book Blog

PHP Reference: Beginner to Intermediate PHP5

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%20Lurig&gender=male%21

or

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!

Similar Posts:

Be Sociable, Share!

Comments are closed.