Search notes:

PHP code snippets: The $_GET super global variable

$_GET is an associative array (aka hash or dictionary) that contains the key/value pairs using query strings (sometimes also referred to as URL parameters).
In GET methods, a query string is initiated with a question mark ?. The following URL has one key, val_1 with the value one:
https://foo.xy/path/to/something?val_1=one
In order to add multiple key/value pairs, the pairs need to be separated by &:
https://foo.xy/path/to/something?val_1=one&val_2=two
$_GET can be used on all request methods that use query strings, not only GET methods.
The following example prints the values of the query parameters val_1 and val_2 and uses a <form> with <input> to assign new values them:
<html>
<head>
  <title>PHP $_GET test</title>
</head>
<body>

  <?php

    $val_1=array_key_exists('val_1', $_GET) ? $_GET['val_1'] : '';
    $val_2=array_key_exists('val_2', $_GET) ? $_GET['val_2'] : '';

    echo "val_1: " . $val_1 . "<br>";
    echo "val_2: " . $val_2 . "<br>";

  ?>

  <p>

  <form action='#' method='get' style='background-color:#f93'>
     Val 1: <input type='text' name='val_1' value='<?php echo $val_1 ?>'><br>
     Val 2: <input type='text' name='val_2' value='<?php echo $val_2 ?>'><br>
    <input type='submit' value='go!'>
  </form>

</body>
</html>
Github repository about-php, path: /_GET.html

See also

$_POST and $_REQUEST
$_SERVER['QUERY_STRING']
Other PHP snippets.

Index