php -S runs a lightweight development server, optionally with a router script.
What It Does and When to Use It
Start a development server bound to local loopback. Direct PHP code exposes the underlying operation clearly; a framework can supply lifecycle hooks, dependency wiring and retries.
Requirements, Inputs and Expected Behavior
Confirm the runtime, extension, environment variables, and permissions before running the examples in production. Never expose it to an untrusted network as a production web server.
Example 1: Basic Operation
Start a development server bound to local loopback.
php -S 127.0.0.1:8080 -t public
Example 2: Behavior and Validation
Use a lightweight router script for local experiments.
<?php
$path = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
if ($path === '/health') {
header('Content-Type: application/json');
echo json_encode(['ok' => true]);
return;
}
http_response_code(404); echo 'Not Found';
Practical Example: Application Use
Test the local endpoint, not a public production listener.
php -S 127.0.0.1:8080 -t public router.php
curl --fail http://127.0.0.1:8080/health
Important Behavior and Edge Cases
The second example highlights: Use a lightweight router script for local experiments. The practical example adds: Test the local endpoint, not a public production listener.
Comparison with Related Approaches
| Approach | What changes |
|---|---|
| Direct operation versus framework-managed integration | Direct PHP code exposes the underlying operation clearly; a framework can supply lifecycle hooks, dependency wiring and retries. |
| Choosing safely | Choose based on deployment requirements and verify actual behavior rather than assuming defaults. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming successful execution proves the result is usable | Check the return value, required extension, and observable output of the specific operation. |
| Ignoring runtime and environment differences | Verify PHP version, SAPI, operating system, service access, and permissions before deployment. |
| Skipping operational constraints | Never expose it to an untrusted network as a production web server. |
Key Takeaways
- php -S runs a lightweight development server, optionally with a router script.
- Test the local endpoint, not a public production listener.
- Choose based on deployment requirements and verify actual behavior rather than assuming defaults.