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:



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