Stops execution immediately, optionally reporting a message or exit code.
What Is exit() in PHP?
Stops execution immediately, optionally reporting a message or exit code.
Its syntax is:
exit(string|int $status = 0): never
Parameters:
$status— Accepted as string|int; optional default 0.
Return type: never. exit skips following code. Numeric status is meaningful in CLI; avoid exposing internal details as a response message.
Availability: Core PHP language construct.
Example 1: Basic Usage
<?php
if (false) { exit("Stopped"); } echo "Still running";
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. exit skips following code. Numeric status is meaningful in CLI; avoid exposing internal details as a response message.
Example 2: A Different Input
<?php
if (false) { exit(1); } echo "No exit requested";
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. exit skips following code. Numeric status is meaningful in CLI; avoid exposing internal details as a response message.
Practical Example: Applying It in a Program
<?php
$token = "example"; if ($token === "") { http_response_code(400); exit("Missing token"); } echo "Request accepted";
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. exit skips following code. Numeric status is meaningful in CLI; avoid exposing internal details as a response message.
Important Behavior and Edge Cases
- Key behavior: exit skips following code. Numeric status is meaningful in CLI; avoid exposing internal details as a response message.
- Environment: Core PHP language construct.
Comparison With Related Functions
| Function | Purpose |
|---|---|
exit() | Stops execution immediately, optionally reporting a message or exit code. |
die | die and exit are aliases and both terminate script execution. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: exit skips following code | exit skips following code. Numeric status is meaningful in CLI; avoid exposing internal details as a response message. |
Key Takeaways
- Stops execution immediately, optionally reporting a message or exit code.
- exit skips following code. Numeric status is meaningful in CLI; avoid exposing internal details as a response message.
- Core PHP language construct.
Summary: exit skips following code. Numeric status is meaningful in CLI; avoid exposing internal details as a response message.