web php

Request/Response Model

A junior PHP developer should be able to look at a request and answer: what method was used, what path was requested, what input was sent, what response status should come back, what headers are needed, and what body should be returned.

Request parts

An HTTP request usually includes:

  • method, such as GET, POST, PUT, or DELETE
  • path, such as /products/42
  • query string, such as ?page=2
  • headers, such as Accept or Content-Type
  • cookies
  • body data, such as form fields or JSON
  • uploaded files

PHP exposes this information through superglobals such as $_SERVER, $_GET, $_POST, $_COOKIE, and $_FILES. Frameworks wrap these in request objects, but the underlying ideas are the same.

PHP example
<?php

declare(strict_types=1);

$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
$path = parse_url($_SERVER['REQUEST_URI'] ?? '/courses?track=php', PHP_URL_PATH);

echo "Request: {$method} {$path}" . PHP_EOL;

// Output from CLI defaults:
// Request: GET /courses

Response parts

An HTTP response usually includes:

  • status code, such as 200, 302, 404, or 500
  • headers, such as Content-Type or Location
  • body, such as HTML, JSON, or an empty response

In plain PHP, you can set the status with http_response_code(), set headers with header(), and write the body with echo.

PHP example
<?php

declare(strict_types=1);

http_response_code(404);
header('Content-Type: text/plain; charset=UTF-8');

echo 'Page not found' . PHP_EOL;

// Output body:
// Page not found

Headers must be sent before the response body. If code echoes output first and then tries to redirect or set cookies, PHP may warn that headers have already been sent.

A tiny router-style example

This example decides a response from method and path:

PHP example
<?php

declare(strict_types=1);

function responseFor(string $method, string $path): array
{
    if ($method === 'GET' && $path === '/') {
        return [200, 'Welcome'];
    }

    if ($method === 'GET' && $path === '/health') {
        return [200, 'OK'];
    }

    return [404, 'Not found'];
}

[$status, $body] = responseFor('GET', '/health');

echo $status . ' ' . $body . PHP_EOL;

// Prints:
// 200 OK

Framework routers do more than this, but the core decision is the same: read the request, choose application behaviour, then build a response.

Common mistakes

Do not treat every request as a successful GET. A form submission, JSON API call, redirect, validation failure, missing route, and server error should not all return the same response.

Do not trust request input just because it came from a browser. Query strings, form fields, cookies, headers, and JSON bodies can all be changed by the client.

Do not print debugging output before setting headers, redirects, sessions, or cookies. That can break the response.

What you should be able to do

After this lesson, you should be able to describe the parts of an HTTP request and response, read method and path in PHP, choose a basic status code, and explain why web applications are structured around request-in and response-out.

Practice

Task: Map Requests To Responses

Create a small PHP function that maps an HTTP method and path to a response.

Requirements

  • Accept a method and path as function arguments.
  • Return a status code and body.
  • Handle GET /, GET /health, and an unknown route.
  • Include at least one 404 response.
  • Add a short note explaining which parts are the request and which parts are the response.

Check your work

The example should run from the command line without needing a real web server, but it should still use realistic HTTP words: method, path, status, headers, and body.

Show solution
PHP example
<?php

declare(strict_types=1);

/**
 * @return array{status: int, body: string}
 */
function responseFor(string $method, string $path): array
{
    if ($method === 'GET' && $path === '/') {
        return ['status' => 200, 'body' => 'Welcome'];
    }

    if ($method === 'GET' && $path === '/health') {
        return ['status' => 200, 'body' => 'OK'];
    }

    return ['status' => 404, 'body' => 'Not found'];
}

$examples = [
    ['GET', '/'],
    ['GET', '/health'],
    ['POST', '/health'],
];

foreach ($examples as [$method, $path]) {
    $response = responseFor($method, $path);

    echo $method . ' ' . $path . ' -> ';
    echo $response['status'] . ' ' . $response['body'] . PHP_EOL;
}

// Prints:
// GET / -> 200 Welcome
// GET /health -> 200 OK
// POST /health -> 404 Not found

The request information is the method and path. The response information is the status code and body. A real web response can also include headers such as Content-Type, Location, and Set-Cookie.