How to Get URL Parameter in PHP
Getting GET parameters:
If the URL has GET parameters, so it looks like, for example:
localhost/snippetsdb/?name=Anna
or
localhost/snippetsdb/?name=Anna&age=31
We can simply refer to GET values using $_GET[‘name‘], as in the following example:
<?php
echo $_GET['name'] . ' is ' . $_GET['age'] . ' years old.';
?>
Output: Anna is 31 years old.
Getting other URL parameters:
If we don’t have access to GET parameters, we can get all URL parameters using parse_url function. However, in the first parameter we have to specify the URL (or only its parameters) we want to check.
<?php
$url = parse_url( $_SERVER['REQUEST_URI']);
var_dump($url);
?>
Output for localhost/snippetsdb/?name=Anna&age=31:
array(2) { ["path"]=> string(12) "/snippetsdb/" ["query"]=> string(16) "name=Anna&age=31" }