In PHP, superglobals are built-in global arrays that provide access to certain data types, such as form inputs, session data, and URL parameters. Among the most commonly used superglobals in web development are $_GET and $_POST. These superglobals are used to collect data from HTML forms and URLs.
In this article, we will explore what $_GET and $_POST are, how they work, and their use cases in PHP programming.
What is $_GET ?
The $_GET superglobal is used to collect form data sent via the GET method. This method appends data to the URL as query parameters, which are visible to users in the browser’s address bar. Some of the features of the $_GET are:
- Retrieves data sent to the server as part of the URL query string.
- Useful for retrieving data that is visible to the user, such as search queries or parameters in a URL.
- Data sent via
$_GET
is visible in the URL, making it less secure for sensitive information. - Accessed using associative array notation, e.g.,
$_GET['parameter']
.
Syntax:
<?php echo $_GET['username']; // Outputs the value of the 'username' parameter from the URL ?>
Now, let us understand with the help of the example:
PHP <html> <body> <form action="index.php" method="get"> <label>username:</label><br> <input type="text" name="username"><br> <input type="submit" value="Log in"><br> </form> </body> </html> <?php echo "{$_GET["username"]} <br>" ; ?>
Output
$_GET SuperglobalsIn this example:
- A form asks the user for a username and submits it via the GET method to index.php.
- The form submits the data to the same page (index.php), allowing the same script to process the data.
- The form includes a text input field for the username.
- The submit button sends the form data when clicked.
- After submission, PHP retrieves and displays the entered username using $_GET['username'].
- Since the form uses the GET method, the form data (like the username) is appended to the URL as query parameters (e.g., index.php?username=anjali), making the username visible in the browser’s URL bar.
What is $_POST?
The $_POST superglobal is used to collect form data sent via the POST method. Unlike GET, the POST method sends data in the body of the HTTP request, so the data is not visible in the URL. Some of the features of the $_POST are:
- Retrieves data sent to the server in the body of an HTTP request, typically from HTML forms submitted using the POST method.
- Suitable for handling sensitive or large amounts of data as it's not visible in the URL.
- More secure than
$_GET
for handling sensitive information like passwords. - Accessed using associative array notation, e.g.,
$_POST['field_name']
.
Syntax:
<?php echo $_POST['username']; // Outputs the value of the 'username' parameter submitted via POST ?>
Now, let us understand with the help of the example:
PHP <html> <body> <form action="index.php" method="post"> <label>username:</label><br> <input type="text" name="username"><br> <input type="submit" value="Log in"><br> </form> </body> </html> <?php echo "{$_POST["username"]} <br>" ; ?>
Output
$_POST SuperglobalsIn this example:
- A form asks the user for a username and submits it via the POST method to index.php.
- The form submits the data to the same page (index.php), ensuring the data is processed on the same script.
- The form includes a text input field for the username.
- The submit button sends the form data when clicked.
- After submission, PHP retrieves and displays the entered username using $_POST['username'].
- Since the form uses the POST method, the form data is sent in the HTTP request body, not in the URL, so the username is not visible in the URL bar.
When to Use $_GET and $_POST
Use $_GET:
- For data like search terms or filter criteria that don't need security.
- Best for short data (due to URL length limits).
- Useful when you need to share or bookmark URLs with parameters (e.g., search queries).
- Typically used for retrieving data without modifying the server (e.g., reading data from a database).
Use $_POST:
- Sensitive information (e.g., passwords, payment details) since it is not visible in the URL.
- Suitable for forms with larger data, such as file uploads.
- Use for submitting forms that change server-side data (e.g., registration forms).
- Prevents data from being cached or stored in browser history.
$_GET vs $_POST
Both $_GET and $_POST are superglobals in PHP used to collect form data, but they differ in how and where the data is transmitted.
$_GET | $_POST |
---|
Data is visible in the URL | Data is hidden in the HTTP request body |
Limited by URL length | Can handle large data sizes |
Not secure (data is visible) | More secure, but still requires sanitization |
Similar Reads
HTTP GET and POST Methods in PHP In this article, we will know what HTTP GET and POST methods are in PHP, how to implement these HTTP methods & their usage, by understanding them through the examples. HTTP: The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers. HTTP works as a r
4 min read
PHP Date and Time PHP provides functions to work with dates and times, allowing developers to display the current date/time, manipulate and format dates, and perform operations like date comparisons, time zone adjustments, and more.In this article, we'll discuss PHP date and time.Why are Date and Time Important in PH
5 min read
How to use PHP in HTML ? In this article, we will use PHP in HTML. There are various methods to integrate PHP and HTML, some of them are discussed below. You can add PHP tags to your HTML Page. You simply need to enclose the PHP code with the PHP starts tag <?php and the PHP end tag ?>. The code wrapped between these
2 min read
Variables and Datatypes in PHP A variable in PHP is a container used to store data. The value of a variable can change during program execution. PHP variables start with a dollar sign ($), followed by the variable name.$name = "GFG";$age = 30;Declaring Variables in PHPTo declare a variable, you simply use the dollar sign followed
4 min read
How to upload a file in PHP ? In this article, we will learn how to upload a file using PHP. Let us first understand some basic configurations. Approach: In your "php.ini" file, search for the "file_uploads" parameter and set it to "On" as mentioned below. file_uploads = On In the "index.html" file, the enctype must be multipart
3 min read