PHP $_GET and $_POST


$_GET and $_POST serve as PHP superglobals which act as built-in associative arrays to automatically gather data from submitted HTML forms.

 


Superglobal Description
$_GET collects data transmitted through the URL using the query string method.
$_POST fetches data transmitted through the HTTP POST method

Using $_GET

HTML Form (GET Method)

<form action="get_example.php" method="GET">
  Name: <input type="text" name="username">
  <input type="submit" value="Submit">
</form>  

PHP Script: get_example.php

<?php
$name = $_GET['username'];
echo "Hello, $name!";
?>

URL after submission

http://example.com/get_example.php?username=Alice 

Using $_POST

HTML Form (POST Method)

<form action="post_example.php" method="POST">
  Name: <input type="text" name="username">
  <input type="submit" value="Submit">
</form>  

PHP Script: post_example.php

<?php
$name = $_POST['username'];
echo "Hello, $name!";
?>  

notepad
  • All the data remains hidden from the URL.
  • More secure and better for passwords, login, and large data

Difference between $_GET and $_POST

Feature $_GET $_POST
Data Visibility Visible in URL Hidden from URL
Max Length Limited by URL length No significant limit
Use Case Search, filters, navigation Login forms, file uploads, etc.
Security Less secure (data in URL) More secure (data in body)
Bookmarkable Yes No



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.