Get URL query string parameters
To get the query string parameters in PHP, you can use the $_GET
superglobal array. This array contains key-value pairs of the query string parameters.
For example, if the URL of the page is http://example.com/page.php?key1=value1&key2=value2
, you can access the values of the query string parameters like this:
<?php
echo $_GET['key1']; // Outputs: value1
echo $_GET['key2']; // Outputs: value2
Watch a video course
Learn object oriented PHP
You can also use the parse_str()
function to parse the query string and store it in an associative array. Here's an example:
<?php
$query = parse_str($_SERVER['QUERY_STRING'], $parameters);
echo $parameters['key1']; // Outputs: value1
echo $parameters['key2']; // Outputs: value2
Note that if the parameter is not present in the query string, $_GET
will return null
. You can use the isset()
function to check if a parameter is set.
<?php
if (isset($_GET['key1'])) {
echo 'key1 is set';
}