Determining the HTTP Request Type in PHP: GET, POST, PUT, DELETE — A Complete Breakdown

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why is it important to know what request came to the server?

\n\n

Imagine you are a doorman at an elite building. Different people come to you: some just want to view the list of residents (GET), others want to add a new resident (POST), others want to update data (PUT), and others want to delete a record (DELETE). If you mix up the actions, chaos will ensue. Roughly the same thing happens in PHP when you build a modern web application or a REST API.

\n\n

The question "Detecting request type in PHP (GET, POST, PUT or DELETE)" is one of the most popular on Stack Overflow (thousands of views). And this is no coincidence. Beginners often write: if ($_POST) — and catch bugs when a PUT or DELETE request arrives. Today, we will once and for all figure out how to properly determine the HTTP request method in PHP so that your code is reliable and professional.

\n\n

The article is written for the educational website PythonLib, but the topic is universal. Let's go!

\n\n

What are HTTP methods and why distinguish between them?

\n

HTTP is the protocol by which a browser (or client) communicates with a server. Each request has a method (verb) that tells the server what exactly needs to be done:

\n
    \n
  • GET — retrieve data (read). Safe, idempotent.
  • \n
  • POST — send data (create a new resource).
  • \n
  • PUT — completely replace a resource (update).
  • \n
  • DELETE — delete a resource.
  • \n
  • PATCH — partially update (less common).
  • \n
\n

In classic HTML forms, the browser only supports GET and POST. However, in REST APIs (for example, for mobile applications or AJAX), all methods are actively used. If you are writing a universal backend, you need to be able to distinguish between them.

\n\n

The most important tool: $_SERVER['REQUEST_METHOD']

\n

In PHP, all information about the request is stored in the superglobal array $_SERVER. The key REQUEST_METHOD contains a string with the method name: 'GET', 'POST', 'PUT', 'DELETE', etc.

\n\n

Here is a basic example:

\n
<?php\n$method = $_SERVER['REQUEST_METHOD'];\necho "Request type: " . $method;\n?>
\n

Run this code on your server. If you open the page in a browser, you will see GET. If you submit a form with method="POST", you will see POST. It's brilliantly simple.

\n\n

But this is just the beginning. Let's break down how to properly build logic based on the method.

\n\n

Practical example: A router for a REST API

\n

Let's say we are writing a simple API for managing a to-do list. We have one endpoint — /api/tasks. Depending on the request method, we perform different actions:

\n
    \n
  • GET — return a list of all tasks.
  • \n
  • POST — create a new task.
  • \n
  • PUT — update an existing task (replace entirely).
  • \n
  • DELETE — delete a task.
  • \n
\n\n

Here is what it looks like in code:

\n
<?php\n// Determine the request method\n$method = $_SERVER['REQUEST_METHOD'];\n\n// Routing\nswitch ($method) {\n    case 'GET':\n        // Handle GET request\n        echo "Displaying list of tasks...";\n        // Logic for retrieving data from DB here\n        break;\n\n    case 'POST':\n        // Handle POST request\n        echo "Creating a new task...";\n        // $_POST contains form data\n        $title = $_POST['title'] ?? '';\n        // Logic for saving to DB\n        break;\n\n    case 'PUT':\n        // Handle PUT request\n        echo "Updating task...";\n        // IMPORTANT: PUT data does not go into $_POST!\n        // It needs to be read from php://input\n        $pu                

Blogs

Book Recommendations