Question
In PHP, how can I determine which HTTP request method was used for the current request, such as GET, POST, PUT, or DELETE?
Short Answer
By the end of this page, you will understand how PHP exposes the current HTTP request method, how to check for methods like GET, POST, PUT, and DELETE, and how this is commonly used in form handling, APIs, routing, and request validation.
Concept
PHP provides information about the current HTTP request through the $_SERVER superglobal. To detect the request type, you usually read $_SERVER['REQUEST_METHOD'].
$method = $_SERVER['REQUEST_METHOD'];
This value contains the HTTP method used by the client, such as:
GETPOSTPUTDELETEPATCHOPTIONS
This matters because web applications often behave differently depending on the request method:
GETusually retrieves dataPOSTusually creates or submits dataPUTusually updates or replaces dataDELETEusually removes data
In real programming, checking the request method is a basic part of:
- building APIs
- processing forms
- writing simple routers
- validating allowed actions
- returning proper error responses
The key idea is that the URL alone is not always enough to decide what the server should do. The HTTP method provides extra meaning about the action being requested.
Mental Model
Think of an HTTP request like a labeled delivery envelope.
- The URL tells you where the envelope should go.
- The request method tells you what kind of action the sender wants.
For example:
GET= “Please show me the information.”POST= “Please send this new information.”PUT= “Please replace or update this information.”DELETE= “Please remove this information.”
In PHP, $_SERVER['REQUEST_METHOD'] is the label on that envelope. Your code reads the label and decides how to handle the request.
Syntax and Examples
The basic syntax is:
$method = $_SERVER['REQUEST_METHOD'];
You can compare it with specific method names:
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
echo 'This is a GET request';
}
Example: Checking multiple request methods
<?php
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
echo 'Fetch data';
} elseif ($method === 'POST') {
echo 'Create data';
} elseif ($method === 'PUT') {
echo 'Update data';
} elseif ($method === 'DELETE') {
echo 'Delete data';
} else {
echo ;
}
Step by Step Execution
Consider this example:
<?php
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'POST') {
echo 'Form submitted';
} else {
echo 'Show form';
}
Step by step:
- PHP receives an HTTP request.
- The web server passes request details to PHP.
- PHP stores the request method in
$_SERVER['REQUEST_METHOD']. - The code copies that value into
$method. - The
ifstatement checks whether$methodis exactly'POST'. - If it is
POST, PHP printsForm submitted. - If it is not
POST(for exampleGET), PHP printsShow form.
Example trace
If the browser visits the page normally:
Real World Use Cases
Checking the request method is common in many PHP applications.
1. Form handling
A page may show a form on GET and process it on POST.
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
echo 'Display form';
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo 'Process submitted data';
}
2. REST-style APIs
An API endpoint may use the same URL with different methods:
GET /users/5→ fetch a userPUT /users/5→ update a userDELETE /users/5→ delete a user
3. Route protection
You may allow only certain methods for an endpoint.
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
();
}
Real Codebase Usage
In real projects, developers usually do more than just print the method. They combine request-method checks with validation, routing, and error handling.
Guard clauses
A common pattern is to reject invalid methods early.
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
exit('Method Not Allowed');
}
This keeps the rest of the code simpler.
Simple routing
A single endpoint may branch by method:
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
// return resource
break;
case 'POST':
// create resource
break;
default:
http_response_code(405);
}
Validation before processing
Developers often check both the method and the input data.
([] !== ) {
();
;
}
(([])) {
();
();
}
Common Mistakes
1. Using $_POST to detect the request method
Broken idea:
if ($_POST) {
echo 'This must be POST';
}
Why it is a mistake:
$_POSTbeing empty does not always mean the request was notPOST- the correct source for the HTTP method is
$_SERVER['REQUEST_METHOD']
Correct version:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo 'This is a POST request';
}
2. Forgetting that method names are uppercase
Broken code:
if ($_SERVER['REQUEST_METHOD'] === 'post') {
echo 'POST request';
}
Why it fails:
- PHP usually provides method names like
GETand in uppercase
Comparisons
| Approach | Best for | Notes |
|---|---|---|
if/elseif with $_SERVER['REQUEST_METHOD'] | Small scripts and simple logic | Easy for beginners to read |
switch ($_SERVER['REQUEST_METHOD']) | Multiple methods on one endpoint | Cleaner when many cases exist |
| Framework routing | Larger applications | Framework handles method matching for you |
GET vs POST vs PUT vs DELETE
| Method | Typical purpose | Common data location |
|---|
Cheat Sheet
// Get the HTTP request method
$method = $_SERVER['REQUEST_METHOD'];
Common checks
if ($method === 'GET') {
// read
}
if ($method === 'POST') {
// create or submit
}
if ($method === 'PUT') {
// update
}
if ($method === 'DELETE') {
// delete
}
Reject unsupported methods
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
exit('Method Not Allowed');
}
Read raw body for PUT or DELETE
$body = file_get_contents('php://input');
Key rules
FAQ
How do I check if a request is GET or POST in PHP?
Use $_SERVER['REQUEST_METHOD'] and compare it to 'GET' or 'POST'.
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// GET request
}
What does $_SERVER['REQUEST_METHOD'] return in PHP?
It returns the HTTP method used for the current request, such as GET, POST, PUT, or DELETE.
Can PHP detect PUT and DELETE requests?
Yes. PHP can detect them through $_SERVER['REQUEST_METHOD']. However, request body data for those methods often needs to be read from php://input.
Why is $_POST empty on a PUT request?
Because $_POST is generally populated for form-style POST requests, not for raw PUT request bodies.
Mini Project
Description
Build a small PHP endpoint that behaves differently depending on the HTTP request method. This demonstrates how one URL can support multiple actions, which is a common pattern in APIs and backend services.
Goal
Create a PHP script that detects GET, POST, PUT, and DELETE requests and returns an appropriate message for each one.
Requirements
- Read the current request method using PHP.
- Return a different response for
GET,POST,PUT, andDELETE. - For
PUT, read and display the raw request body. - Return a
405status code for unsupported methods.
Keep learning
Related questions
Converting HTML and CSS to PDF in PHP: Core Concepts, Limits, and Practical Approaches
Learn how HTML-to-PDF conversion works in PHP, why CSS support varies, and how to choose practical approaches for reliable PDF output.
How PHP foreach Actually Works with Arrays
Learn how PHP foreach works internally, including array copies, internal pointers, by-value vs by-reference behavior, and common pitfalls.
How to Check String Prefixes and Suffixes in PHP
Learn how to check whether a string starts or ends with specific text in PHP using simple functions and practical examples.