Content · Programming · PHP

PHP connection_aborted() Function: Usage and Examples

Reports whether a web client disconnected from the current PHP request. Under CLI or proxy buffering this may not reflect a browser disconnect until output is attempted.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

Reports whether a web client disconnected from the current PHP request.

The examples focus on the operation itself, including the important distinction: connection_status returns the full request connection-state bitmask.

What It Does


Use the examples below to observe the behavior on a small dataset before extending it to application code.

Syntax and Return Value


connection_aborted(): int

The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.

Declared return type: int. Read the behavior notes for important cases such as empty collections and destructive iteration.

Example 1: Basic Use


In real web servers, connection state may be updated only after the server attempts to send output; CLI is not a browser simulation.

<?php
echo connection_aborted() ? "client disconnected" : "connection not reported aborted";

Output from a successful local PHP 8.4 run:

connection not reported aborted

Example 2: Checking Behavior


In real web servers, connection state may be updated only after the server attempts to send output; CLI is not a browser simulation. Client abort detection depends on output and server behavior.

<?php
echo "aborted-bit=" . connection_aborted();

Output from a successful local PHP 8.4 run:

aborted-bit=0

Example 3: Practical Example


In real web servers, connection state may be updated only after the server attempts to send output; CLI is not a browser simulation. Client abort detection depends on output and server behavior.

<?php
echo connection_aborted() === 0 ? "connected or not detected" : "aborted";

Output from a successful local PHP 8.4 run:

connected or not detected

Important Behavior and Edge Cases


Under CLI or proxy buffering this may not reflect a browser disconnect until output is attempted.

Comparison with Related Approaches


ApproachDifference
connection_abortedReports whether a web client disconnected from the current PHP request.
Related API or alternativeconnection_status returns the full request connection-state bitmask.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsUnder CLI or proxy buffering this may not reflect a browser disconnect until output is attempted.
Confusing it with a related APIconnection_status returns the full request connection-state bitmask.

Key Takeaways


  • Reports whether a web client disconnected from the current PHP request.
  • Under CLI or proxy buffering this may not reflect a browser disconnect until output is attempted.
  • connection_status returns the full request connection-state bitmask.

Use connection_aborted when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.

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