Stops script execution; die is an alias of exit.
What Is die() in PHP?
Stops script execution; die is an alias of exit.
Its syntax is:
die(string|int $status = 0): never
Parameters:
$status— Accepted as string|int; optional default 0.
Return type: never. An integer argument is an exit status in CLI contexts; a string is printed before termination.
Availability: Core PHP.
Example 1: Basic Usage
<?php
if (false) { die("Stopped"); } echo "Continues";
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. An integer argument is an exit status in CLI contexts; a string is printed before termination.
Example 2: A Different Input
<?php
if (false) { die(1); } echo "No exit requested";
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. An integer argument is an exit status in CLI contexts; a string is printed before termination.
Practical Example: Applying It in a Program
<?php
$requiredValue = "present"; if ($requiredValue === "") { die("Missing value"); } echo "Validated";
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. An integer argument is an exit status in CLI contexts; a string is printed before termination.
Important Behavior and Edge Cases
- Key behavior: An integer argument is an exit status in CLI contexts; a string is printed before termination.
- Environment: Core PHP.
Comparison With Related Functions
| Function | Purpose |
|---|---|
die() | Stops script execution; die is an alias of exit. |
exit | die is an alias of exit; both stop the script. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: An integer argument is an exit status in CLI contexts; a string is printed before termination | An integer argument is an exit status in CLI contexts; a string is printed before termination. |
Key Takeaways
- Stops script execution; die is an alias of exit.
- An integer argument is an exit status in CLI contexts; a string is printed before termination.
- Core PHP.
Summary: An integer argument is an exit status in CLI contexts; a string is printed before termination.