How to use $_GET and $_POST arrays in PHP

1
26062

GET and POST are two essential methods when a web communicates with the server. These methods determine whether the web is posting data to or getting data from the server.

PHP which is a language that was created for making the web, give us a useful way to handle the data in process of Post or Get method. In this post, we’re getting to know about $_GET and $_POST array in PHP.

There’s one thing you need to know that in a .php file, we can write HTML and PHP together, the PHP code must be written in between the tag <php? /*code here*/ ?>;

How to pass variables in URL

First of all, we need to know how to pass some variables or parameters in the URL.

http://localhost/task1.php/?firstName=Sam&lastName=Nguyen

After the normal URL:

  1. To declare the variable, we use “?” mark
  2. then the variable name
  3. “=” sign to assign a value
  4. the last thing is the value itself
  5. If you have multiple variables, separating them by “&” mark.

Using $_GET array in PHP

Now that we know how to pass the variables in the URL, we’re going to get it in PHP using $_GET. $_GET is a built-in variable of PHP which is an array that holds the variable that we get from the URL.

Since we passed some variable in the URL, we should know what is the name of variables to get it using the syntax $_GET[variable_name];

echo $_GET['firstName'] //Sam
echo $_GET['firstName'] //Nguyen
use "echo" to print the value to web page.

Validate variable in URL

There are two things we will consider here: first if the variable is declared, second if the variable is blank.

Using isset() to check if the variable is declared in URL, this is a function that returns boolean true if the parameter is detected in URL false if not. As long as the variable name is in the URL it will return true.

http://localhost/task1.php/?firstName=Sam&lastName=
isset($_GET['firstName'])  // true
isset($_GET['middleName']) // false
isset($_GET['lastName']) // true

As you can see in the code above, the lastName variable hasn’t been provided a valued but it still returns return true since isset() function detect the variable name in URL.

Now we need to check if the variable is empty/blank or not. This is as simple as any other language by comparing the variable to an empty string

http://localhost/task1.php/?firstName=Sam&lastName=
$_GET['firstName'] == "" // false
$_GET['lastName'] == "" // true

Using $_POST array in PHP

To use the Post method, we definitely need a form in our HTML file to handle data we want to post. The PHP built-in variable $_POST is also an array and it holds the data that we provide along with the post method.

Consider the simple application below, we will demand the user to put in 2 number to add them together.

<form method="POST">
    <h1>ADD 2 NUMBER</h1>
    <input type="number" name="num1">
    <input type="number" name="num2">
    <input type="submit">
    <?php
           echo $_POST["num1"] + $_POST['num2']
        ?>
</form>

The form in the code above has 2 data that we want to process that are stored in 2 input fields. Notice that these input fields have different name value. And the value of the name attribute is what we use to access the value of the input fields itself in PHP.

Whenever the post method is triggered by clicking the submit button, PHP will get the value of the variable that we desire to catch.

$_POST["num1"]  // this will hold the value of input field with name="num1"
$_POST['num2'] // this will hold the value of input field with name="num2"

And then, we will echo/ print out the sum onto the web page

echo $_POST["num1"] + $_POST['num2']

Source code for $_POST:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Add Two Numbers</title>
</head>
<body>
<form method="POST">
    <h1>ADD 2 NUMBER</h1>
    <input type="number" name="num1">
    <input type="number" name="num2">
    <input type="submit">
    <?php
           echo $_POST["num1"] + $_POST['num2']
        ?>
</form>
</body>
</html>

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here