PHP Get and Post
Get and post are the two request kinds behind the $_GET and $_POST arrays. A program reads values from both arrays by key.
A greeting from $_GET
A server fills this array from the address. Here the first line fills it by hand to show the shape.
<?php
$_GET["name"] = "Hana";
echo "Hello " . $_GET["name"] . "\n";Two values from $_GET
Two keys are filled by hand. Each value prints on its own line.
<?php
$_GET["name"] = "Hana";
$_GET["city"] = "Oslo";
echo "Name: " . $_GET["name"] . "\n";
echo "City: " . $_GET["city"] . "\n";Two values from $_POST
The form fields become keys in $_POST. The reading lines match the $_GET shape.
<?php
$_POST["name"] = "Hana";
$_POST["plan"] = "Premium";
echo "Name: " . $_POST["name"] . "\n";
echo "Plan: " . $_POST["plan"] . "\n";Example
<?php
$_POST["name"] = "Hana";
$_POST["city"] = "Oslo";
echo "Welcome, " . $_POST["name"] . "\n";
echo "City: " . $_POST["city"] . "\n";Key points
- On a real server, a program starts with these arrays already filled.
- Reading a value uses the array name and its key.
- The array name shows where the values came from.
- Filling an array by hand changes nothing about how it is read.
In this example, two lines fill $_POST by hand with a name and a city. The print lines read those keys from $_POST. The welcome card shows Hana on the first line and Oslo on the second.
Note
The keys are the names from the address or the form fields.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Programiz PHP Compiler
Exercise
In a real request, which array holds the values from a submitted form?