當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP connection_status()用法及代碼示例


connection_status()函數是PHP中的內置函數,可返回當前的連接狀態。

用法:

int connection_status( void )

參數:此函數不接受任何參數。


返回值:此函數返回連接狀態位字段。返回的連接狀態位字段的可能值為:

  • 0:CONNECTION_NORMAL –正常運行
  • 1:CONNECTION_ABORTED –因用戶或網絡錯誤而中止
  • 2:CONNECTION_TIMEOUT –超時
  • 3:CONNECTION_ABORTED和CONNECTION_TIMEOUT –中止並超時

注意:此函數可用於PHP 4.0.0和更高版本。

以下示例程序旨在說明PHP中的connection_status()函數。

示例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; 
} 
?>

輸出:

Connection is in a normal state

示例2:一些輸出將發送到瀏覽器,以便connection_status()在瀏覽器中斷或關閉的情況下起作用。

<?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; 
} 
?>

輸出:

Connection is in a normal state

參考: https://www.php.net/manual/en/function.connection-status.php



相關用法


注:本文由純淨天空篩選整理自gekcho大神的英文原創作品 PHP | connection_status() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。