HTTP GET and POST Methods in PHP with Examples

By Vijay

By Vijay

Image
I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated July 4, 2025
Edited by Kamila

Edited by Kamila

Image
Kamila is an AI-based technical expert, author, and trainer with a Master’s degree in CRM. She has over 15 years of work experience in several top-notch IT companies. She has published more than 500 articles on various Software Testing Related Topics, Programming Languages, AI Concepts,…

Learn about our editorial policies.

Gain a complete understanding on how to use PHP GET and POST methods with examples. Learn how to handle form data effectively with these two popular methods in PHP:

PHP supports different HTTP request methods, like GET, POST, PUT, HEAD, etc.

In this tutorial, our main concentration is on the GET and POST methods. Also, we’ll cover some of the frequently asked questions (FAQs) related to this topic.

Please note that we have used PHP version 7 in all examples.

Let’s begin!

=> Read Through the Series of PHP Tutorials

Introduction to PHP GET and PHP POST

PHP GET And POST

The browser can send information to the web server using two methods.

They are,

  1. The GET method
  2. The POST method

Let’s discuss each of them in detail.

PHP GET Method

The GET method is one of the most common HTTP methods used in PHP. It is used to submit HTML form data to the server. The submitted data are collected by a special variable (superglobal) called $_GET.

Note: $_GET is case sensitive as it is a superglobal.

The GET method is the default method of submitting HTML form data. Furthermore, the GET method is used to request data from a particular resource.

The GET request to a web page is as follows:

http://www.samplesite.com/index.htm?name1=value1&name2=value2&name3=value3...
http://www.example.com/index.htm?first_name=john&last_name=doe

Note: The question mark character (?) is used to separate the page, and the encoded information, while the ampersand sign (&) is used to separate name-value pairs.

Example:

An example is shown below. You can practice this example by running the following programming code:

<form method="get" action="<?php echo $_SERVER["PHP_SELF"]; ?>?">
    Name: <input type="text" id="name" name="name" value="<?php echo @$_GET['name']; ?>">
    <br><br>
    Age: <input type="number" id="age" name="age" value="<?php echo @$_GET['age']; ?>">
    <br><br>
    <button type="submit">Send</button>
</form>
 
<?php
 
echo "<h3>Output:</h3>";
echo @$_GET['name']." ".@$_GET['age'];
 
?>

The following screenshots show the browser output of the above programming code:

  • Before entering values:
programming code
  • After entering values and submitting the form:
submitting the form12

Note: The plus sign (+) is used to encode space.

PHP POST Method

The POST method is another most common HTTP method used in PHP. It is also used to submit HTML form data to the server. The submitted data are collected by a special variable (superglobal) called $_POST.

Note: $_POST is case sensitive, as it is a superglobal.

Furthermore, there are some situations where you can use POST instead of PUT.

Example:

An example is shown below. You can practice this example by running the following programming code:

<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>?">
    Name: <input type="text" id="name" name="name" value="<?php echo @$_POST['name']; ?>">
    <br><br>
    Age: <input type="number" id="age" name="age" value="<?php echo @$_POST['age']; ?>">
    <br><br>
    <button type="submit">Send</button>
</form>
 
<?php
 
echo "<h3>Output:</h3>";
echo @$_POST['name']." ".@$_POST['age'];
 
?>

The following screenshots show the browser output of the above programming code:

  • Before entering values:
localhost
  • After entering values and submitting the form:
test php

PHP GET vs POST

Take a look at the differences between GET requests and POST requests:

ParameterPHP GET RequestPHP POST Request
1VisibilityValues are visible in the URL. Hence, visible to everyone.Values are not visible in the URL. Hence, invisible to everyone.
2SecurityComparatively less secure as values are visible in the URL.Comparatively more secure as values are not visible in the URL.
3BookmarkCan be bookmarked.Cannot be bookmarked.
4CacheCan be cached.Cannot be cached.
5Character limitation2048 characters.No character limitation.
6Data typeCannot be used to send binary data such as images, word documents, audios, videos, etc.Can be used to send binary data such as images, word documents, audios, videos, etc.
7UsabilityCan be used to send non-sensitive data.Can be used to send both sensitive data (such as passwords) and non-sensitive data.

Frequently Asked Questions

1. What are the GET & POST methods in PHP?

They are the two methods by which the browser can send information to the webserver.

2. What is a GET request used for?

It is used to request data from a particular resource.

3. Is POST more secure than GET?

Yes, POST is slightly more secure than GET because the values are not stored in the browser history, and also, the values are not visible in the URL.

4. Is POST requests slower than GET?

Usually, POST requests are slightly slower than GET requests.

5. Can I use POST instead of PUT?

Yes, you may use POST instead of PUT.

6. What is the character limit in the GET method?

The character limit is up to 2048 characters.

7. What is PHP $_ GET and PHP $_ POST?

They are PHP superglobals used to read the submitted HTML form data.

Conclusion

GET and POST are the two most commonly used HTTP request methods in PHP.

The main difference between the two methods is that values are visible in a GET request but not in a POST request. $_GET are HTTP GET variables while $_POST are HTTP POST variables.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

READ MORE FROM THIS SERIES:



    Leave a Comment