Lesson 32 of 50 – PHP GET Method
64%

PHP GET Method

The HTTP GET method is commonly used to request information from a server. In PHP, values sent through URL query parameters can be accessed using the $_GET superglobal array.

1. What Is the GET Method?

The GET method sends parameters through the URL. These parameters appear after a question mark (?) and are separated by ampersands (&).

Example URL:

http://localhost/example.php?name=Rahul&city=Patna

In this URL:

  • name is a parameter with value Rahul.
  • city is a parameter with value Patna.

PHP can read these values using $_GET.

2. Reading GET Parameters in PHP

The $_GET array contains query parameters from the current URL.

<?php
$name = $_GET['name'] ?? '';
$city = $_GET['city'] ?? '';

echo "Name: " .
    htmlspecialchars($name, ENT_QUOTES, 'UTF-8');

echo "<br>";

echo "City: " .
    htmlspecialchars($city, ENT_QUOTES, 'UTF-8');
?>

Open the page with this URL:

http://localhost/example.php?name=Rahul&city=Patna
Output:
Name: Rahul
City: Patna

The null coalescing operator (??) provides an empty string when a parameter is missing or null. htmlspecialchars() helps prevent HTML injection when displaying text.

3. Creating a GET Form

An HTML form can use method="get" to send its fields as URL query parameters.

<form method="get" action="">
    <label for="studentName">Enter Name:</label>
    <input type="text" id="studentName" name="student_name" required>

    <br><br>

    <label for="studentCity">Enter City:</label>
    <input type="text" id="studentCity" name="student_city" required>

    <br><br>

    <button type="submit">Submit</button>
</form>

The input name attributes determine the keys that PHP receives in $_GET.

For example, submitting Rahul and Patna may produce a URL similar to:

example.php?student_name=Rahul&student_city=Patna
4. Complete GET Form Example

Save this complete code as student_get.php. It displays a form and processes its submitted values on the same page.

<?php
$name = '';
$city = '';

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $name = trim($_GET['student_name'] ?? '');
    $city = trim($_GET['student_city'] ?? '');
}
?>

<form method="get" action="">
    <label for="student_name">Student Name:</label>
    <input
        type="text"
        id="student_name"
        name="student_name"
        required
    >

    <br><br>

    <label for="student_city">City:</label>
    <input
        type="text"
        id="student_city"
        name="student_city"
        required
    >

    <br><br>

    <button type="submit">Submit</button>
</form>

<?php
if ($name !== '' && $city !== '') {
    echo "<h3>Student Details</h3>";

    echo "Name: " .
        htmlspecialchars($name, ENT_QUOTES, 'UTF-8');

    echo "<br>City: " .
        htmlspecialchars($city, ENT_QUOTES, 'UTF-8');
}
?>

After submitting the form, the page displays the student's name and city.

GET requests can also occur when someone opens the page directly. This example processes the form only when both values are non-empty.

5. GET Method with Search

GET is useful for search forms because search terms can be shared through a URL or bookmarked.

<?php
$search = trim($_GET['search'] ?? '');
?>

<form method="get" action="">
    <input
        type="text"
        name="search"
        placeholder="Search course..."
        value="<?= htmlspecialchars(
            $search,
            ENT_QUOTES,
            'UTF-8'
        ) ?>"
    >

    <button type="submit">Search</button>
</form>

<?php
if ($search !== '') {
    echo "<p>You searched for: " .
        htmlspecialchars($search, ENT_QUOTES, 'UTF-8') .
        "</p>";
}
?>

If the user searches for PHP, the URL may contain:

search.php?search=PHP
Output:
You searched for: PHP

The form keeps the search term in its input after submission. The value is escaped before being placed in the HTML attribute.

6. GET Method with Calculator

GET can be used for simple calculations where sharing the input values in the URL is useful.

<?php
$result = null;
$a = $_GET['a'] ?? '';
$b = $_GET['b'] ?? '';

if (
    $_SERVER['REQUEST_METHOD'] === 'GET' &&
    $a !== '' &&
    $b !== '' &&
    is_numeric($a) &&
    is_numeric($b)
) {
    $result = (float)$a + (float)$b;
}
?>

<form method="get" action="">
    <label for="a">First Number:</label>
    <input type="number" step="any" id="a" name="a" required>

    <br><br>

    <label for="b">Second Number:</label>
    <input type="number" step="any" id="b" name="b" required>

    <br><br>

    <button type="submit">Add Numbers</button>
</form>

<?php
if ($result !== null) {
    echo "Sum: " . $result;
}
?>

Submitting 10 and 20 gives a URL similar to ?a=10&b=20 and displays:

Sum: 30

This example checks that both values are numeric. Real applications should also enforce reasonable input ranges and handle any special numeric cases required by the application.

7. GET Method with Dropdown

A dropdown list can send a selected course through the GET method.

<?php
$course = $_GET['course'] ?? '';

$allowedCourses = [
    'PHP',
    'JavaScript',
    'Python'
];

if (!in_array($course, $allowedCourses, true)) {
    $course = '';
}
?>

<form method="get" action="">
    <label for="course">Select Course:</label>

    <select name="course" id="course" required>
        <option value="">Choose a course</option>
        <option value="PHP">PHP</option>
        <option value="JavaScript">JavaScript</option>
        <option value="Python">Python</option>
    </select>

    <button type="submit">View Course</button>
</form>

<?php
if ($course !== '') {
    echo "Selected Course: " .
        htmlspecialchars($course, ENT_QUOTES, 'UTF-8');
}
?>

Selecting PHP displays:

Selected Course: PHP

The server checks the selected value against an allowed list instead of trusting the query parameter.

8. Difference Between GET and POST

GET and POST are HTTP methods used for different purposes when communicating with a server.

GET POST
Parameters are commonly sent in the URL. Form data is commonly sent in the request body.
Useful for searching and retrieving information. Commonly used for submitting or changing data.
URLs can be bookmarked and shared with query values. Submitted body data is not normally part of the URL.
Query values may be stored in browser history and server logs. Body data may also be logged; POST is not automatically secure.
Use $_GET in PHP. Use $_POST in PHP for ordinary URL-encoded or multipart form fields.

Neither method encrypts data. Use HTTPS to protect data in transit. Avoid placing passwords or other sensitive information in GET URLs.

9. Practical Example: Student Search

This example demonstrates how a GET form can search a small list of students. In a database application, the search term would be used in a prepared SQL query.

<?php
$students = [
    "Rahul",
    "Amit",
    "Priya",
    "Neha",
    "Ravi"
];

$search = trim($_GET['student'] ?? '');
$results = [];

if ($search !== '') {
    foreach ($students as $student) {
        if (stripos($student, $search) !== false) {
            $results[] = $student;
        }
    }
}
?>

<form method="get" action="">
    <label for="student">Search Student:</label>

    <input
        type="text"
        id="student"
        name="student"
        value="<?= htmlspecialchars(
            $search,
            ENT_QUOTES,
            'UTF-8'
        ) ?>"
        placeholder="Enter student name"
    >

    <button type="submit">Search</button>
</form>

<?php
if ($search !== '') {
    echo "<h4>Search Results</h4>";

    if (count($results) > 0) {
        echo "<ul>";

        foreach ($results as $student) {
            echo "<li>" .
                htmlspecialchars(
                    $student,
                    ENT_QUOTES,
                    'UTF-8'
                ) .
                "</li>";
        }

        echo "</ul>";
    } else {
        echo "No students found.";
    }
}
?>

Searching for ra matches Rahul and Ravi because the example performs a case-insensitive substring search.

Practice Assignments
  1. Create a GET form that accepts a student's name.
  2. Accept a name and city through GET parameters and display them safely.
  3. Create a course search form using GET.
  4. Build an addition calculator using GET.
  5. Create a dropdown to select a course and display the selection.
  6. Create a form that accepts a number and displays its square.
  7. Create a student search page using an array of names.
  8. Display the current request method using $_SERVER.
  9. Explain three differences between GET and POST.

🧠 Quick Quiz

1. Which PHP superglobal reads URL query parameters?




2. Where are GET parameters commonly visible?




3. Which method is commonly suitable for a search form?