Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

$_GET

PHP $_GET

$_GET contains an array of variables received via the HTTP GET method.

Variables can be sent using the HTTP GET method in two main ways:

  1. Query strings in the URL
  2. HTML forms

Query string in the URL

A query string is additional data appended at the end of a URL. In the link below, everything following the ? sign constitutes the query string:

<a href="demo_phpfile.php?subject=PHP&web=W3schools.com">Test $GET</a>

The query string above includes two key/value pairs:

  • subject=PHP
  • web=W3schools.com

In the PHP file, we can use the $_GET variable to retrieve the values from the query string.

Example

The PHP file demo_phpfile.php:

<html>
<body>
<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>
</body>
</html>

 

$_GET in HTML Forms

An HTML form submits information via the HTTP GET method if the form’s method attribute is set to “GET”.

To illustrate this, let’s create a simple HTML form:

HTML Form

<html>
<body>
<form action="welcome_get.php" method="GET">
  Name: <input type="text" name="name">
  E-mail: <input type="text" name="email">
 <input type="submit">
</form>

</body>
</html>

When a user clicks the submit button, the form data is sent to a PHP file specified in the action attribute of the <form> tag.

The form fields are sent to the PHP file as query strings with your input.

welcome_get.php?name=John&[email protected]

In the action file, we can use the $_GET variable to retrieve the values of the input fields sent as query strings.

Example

PHP code inside the welcome_get.php page:

<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>

Remember to prioritize security when processing PHP forms!

The example above demonstrates sending and retrieving form data without incorporating any form validation measures.