PHP - Accessing Data in Query Strings

Introduction

To access the field names and values in a query string, read them from the $_GET super global array, just as if you were handling a form sent with the get method:

Consider the following code:

<?php
$firstName ="Tom" ;
$homePage ="http://www.example.com/" ;
$favoriteSport ="PHP coding" ;
$queryString ="firstName=". urlencode($firstName)." &amp;homePage=".urlencode($homePage)." & amp;favoriteSport=". urlencode($favoriteSport);
echo'<p>  <a href="moreinfo.php?'. $queryString.'"> Find out more info onthis person </a>  </p>';
?>

You can use the following code to get the parameter value.

$firstName = $_GET["firstName" ];
$homePage = $_GET["homePage" ];

A simple version of the moreinfo.php script referenced in the previous example:

<?php
$firstName = $_GET[" firstName" ];
$homePage = $_GET[" homePage" ];
$favoriteSport = $_GET[" favoriteSport" ];

echo " <dl>" ;
echo " <dt> First name: </dt>  <dd> $firstName </dd>" ;
echo " <dt> Home page: </dt>  <dd> $homePage </dd>" ;
echo " <dt> Favorite sport: </dt>  <dd> $favoriteSport </dd>" ;
echo " </dl>" ;
?>

Related Topic