Content · Programming · PHP

PHP exit() Function: Usage and Examples

Learn how PHP exit works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

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

FunctionPurpose
exit()Stops execution immediately, optionally reporting a message or exit code.
diedie and exit are aliases and both terminate script execution.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: exit skips following codeexit 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.

Share this resourceSend this page to someone who may find it useful.