The Best Way to Understand PHP GET Method With an Example

There are two methods in PHP to collect data submitted in a FORM. GET Method and POST Method. 

In the GET method, you submit data from HTML FORM/collected using a super global variable $_GET. This method sends the encoded information from the FORM through the webpage URL. 

Example: http://localhost/tutorial/get.php?name=Mac&age=21

Full Stack Java Developer Course

In Partnership with HIRIST and HackerEarthEXPLORE COURSE
Full Stack Java Developer Course

Advantages of Using GET Method

Some of the advantages of using the GET Method are:

  • Since the FORM data sent by the GET Method is appended in the URL, the webpage can be bookmarked with specific query string values.
  • Any request made using GET Method remains in the browser history.
  • GET Method requests can be cached.

Disadvantages of Using GET Method

Some of the disadvantages of using this method are:

  • Since the data sent by the GET method is displayed in the URL of the webpage, it is not recommended to use GET Method while sending sensitive information.
  • The GET method has a limitation of 2048 characters while sending data.

PHP GET Method With an Example

To understand how $_GET collects data, you must create a simple FORM with two fields- name and age and collect and display that data using the GET Method.

<?php

    // Below code checks if Name and Age have values. 

   if(isset($_GET["name"]) || isset($_GET["age"]) ) {

      echo "Hi ". $_GET['name']. "<br />";

      echo "Age: ". $_GET['age']. " years old.";

      exit();

   }

?>

<html>

   <body>

      <form action = "<?php $_PHP_SELF ?>" method = "GET">

         Name: <input type = "text" name = "name" />

         Age: <input type = "text" name = "age" />

         <input type = "submit" />

      </form>     

   </body>

</html>

In the above code, the form method has been set to GET and the PHP script collects the data using $_GET superglobal variable.

FREE Java Certification Training

Learn A-Z of Java like never beforeEnroll Now
FREE Java Certification Training

Below is the form with the name and age fields. Notice the URL before submitting the form does not contain any information.

PHP_GET_1

Now, look at the URL of the webpage after submitting the form using the GET Method.

PHP_GET_2

As you can notice in the URL, it displays the information submitted in the form after the ‘?’ symbol.

This is how the GET Method is used to collect and display information.

Looking forward to becoming a PHP Developer? Then get certified with the Advanced PHP Development Course. Enroll now!

Conclusion

This brings us to the end of the “PHP GET Method” tutorial. In this, you have learned what GET Method is and the $_GET superglobal variable. You also saw the advantages and disadvantages of using this method and an example of how the GET Method is used with the help of a simple HTML FORM.

You can refer here for a video tutorial on the GET & POST Methods.

Are you planning to take the plunge and do a course on PHP? In that case, Simplilearn’s PHP course would be an excellent choice. The PHP certification covers all the fundamental and advanced concepts in PHP, making your journey towards learning PHP an easy one.

If you have any queries regarding the PHP GET Method tutorial, do mention it in the comment section of this tutorial, and we’ll have our experts answer them for you.

Happy Learning! 

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors