HTTP protocol's PUT and DELETE and their usage in PHP

HTTP protocol's PUT and DELETE and their usage in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

HTTP Protocol's PUT and DELETE and Their Usage in PHP

The Hypertext Transfer Protocol (HTTP) serves as the backbone of web interactions, governing everything from simple document transfers to complex asynchronous AJAX requests. While most developers are comfortable utilizing POST and GET, many encounter confusion when encountering PUT and DELETE. These methods represent a critical part of the "uniform interface" constraint, providing action counterparts to noun-based resources. Understanding their specific roles is essential for building RESTful APIs that are predictable and scalable.

The primary distinction between these methods lies in how they handle state changes. POST is generally non-idempotent, meaning sending the same request multiple times can have different effects on the server—typically creating multiple new resources if the logic isn't guarded. In contrast, PUT is idempotent. If you send a PUT request to create a resource with specific data, and then send the exact same request again, the result is identical: the resource exists with that specific data. It effectively overwrites the current state of the target resource. This makes PUT ideal for full updates where the client knows the complete payload of the resource.

On the other end of the spectrum sits DELETE. Like PUT, it is idempotent. Deleting a resource once results in its removal; attempting to delete it again yields no error and no further state change, assuming the resource is already gone. This property is crucial for safety mechanisms where clients might retry requests without realizing their previous action succeeded.

In PHP, implementing these methods requires moving beyond the default $_GET and $_POST superglobals. Since there are no built-in $_PUT or $_DELETE variables in standard PHP, you must explicitly inspect the request method using the $_SERVER array. The most common approach involves checking $_SERVER['REQUEST_METHOD'].

Here is a practical example of how to handle these requests in a legacy PHP script:

$method = $_SERVER['REQUEST_METHOD'];

if ($method === 'PUT') {
    // Handle full update logic
    $input = json_decode(file_get_contents('php://input'), true);
    
    if (!empty($input)) {
        // Update the entire resource in your database
        // Example: UPDATE users SET name = ?, email = ? WHERE id = ?
        put_resource($input['id'], $input); 
    } else {
        http_response_code(400);
        echo json_encode(['error' => 'Request body is required']);
    }

} elseif ($method === 'DELETE') {
    // Handle deletion logic
    if (isset($_GET['id'])) {
        $id = $_GET['id'];
        
        // Verify permissions before deleting
        if (isAuthorized($id)) {
            // Remove the resource
            delete_resource($id); 
        } else {
            http_response_code(403);
            echo json_encode(['error' => 'Forbidden']);
        }
    } else {
        http_response_code(404);
        echo json_encode(['error' => 'Resource ID missing']);
    }

} elseif ($method === 'POST') {
    // Handle creation logic
    $input = json_decode(file_get_contents('php://input'), true);
    create_resource($input);
    
} else {
    http_response_code(405); // Method Not Allowed
    echo json_encode(['error' => 'Not supported']);
}

When working with modern frameworks like Laravel, the syntax simplifies significantly. You can utilize route filters or explicit method checking within your controllers. For instance, using the PUT method ensures that only a full update is performed on a specific resource identifier, adhering strictly to REST conventions. Similarly, the DELETE method cleanly removes resources without ambiguity.

A critical trade-off to consider is the difference between partial and full updates. If you need to modify just one field of an existing resource, PUT might be overkill if it requires sending the entire object every time. In such cases, a custom action or PATCH (which supports partial updates) might be more appropriate. However, sticking to standard HTTP verbs like PUT and DELETE ensures your API remains intuitive for frontend developers consuming your endpoints.

For further guidance on building robust RESTful APIs in PHP, you can explore resources such as https://laravelcompany.com, which often details best practices for handling these specific HTTP methods within modern ecosystems.

Our network