PHP | connection_status() Function

The connection_status() function is an inbuilt function in PHP which returns the current connection status.
Syntax:
int connection_status( void )
Parameters: This function doesn’t accept any parameters.
Return Value: This function returns the connection status bitfield. The possible values of returned connection status bitfield are:
- 0:CONNECTION_NORMAL – running normally
- 1:CONNECTION_ABORTED – aborted by user or network error
- 2:CONNECTION_TIMEOUT – timed out
- 3:CONNECTION_ABORTED & CONNECTION_TIMEOUT – aborted and timed out
Note: This function is available for PHP 4.0.0 and newer version.
Below programs illustrate the connection_status() function in PHP.
Program 1:
<?php   switch (connection_status()) {     case CONNECTION_ABORTED:         echo'Connection aborted';         break;     case CONNECTION_TIMEOUT:         echo'Connection timed out';         break;     case CONNECTION_NORMAL:         echo'Connection is in a normal state';         break;       case (CONNECTION_ABORTED & CONNECTION_TIMEOUT):         echo'Connection aborted and timed out';         break;     default:         echo'Unknown';         break; } ?> |
Output:
Connection is in a normal state
Program 2: Some output to be sent to browser for connection_status() to work in case of browser breaks or closed.
<?php   // This will work even if browser breaks or closed // Sending this to client's browser switch (connection_status()) {     case CONNECTION_ABORTED:         echo'Connection aborted';         break;     case CONNECTION_TIMEOUT:         echo'Connection timed out';         break;     case CONNECTION_NORMAL:         echo'Connection is in a normal state';         break;       case (CONNECTION_ABORTED & CONNECTION_TIMEOUT):         echo'Connection aborted and timed out';         break;     default:         echo'Unknown';         break; } ?> |
Output:
Connection is in a normal state
Reference: https://www.php.net/manual/en/function.connection-status.php



