查詢

MessageFormatter::getErrorCode()函式—用法及示例

「 獲取格式化訊息的錯誤碼 」


函式名稱:MessageFormatter::getErrorCode()

適用版本:PHP 5 >= 5.3.0, PHP 7

函式描述:MessageFormatter::getErrorCode() 函式用於獲取格式化訊息的錯誤碼。

用法:

public MessageFormatter::getErrorCode ( void ) : int

引數:該函式沒有引數。

返回值:返回一個整數,表示格式化訊息的錯誤碼。如果返回0,則表示沒有錯誤。

示例:

$message = "Hello, {name}! You have {count, number} new messages.";
$formatter = new MessageFormatter('en_US', $message);

$errorCode = $formatter->getErrorCode();
echo "錯誤碼:" . $errorCode;  // 輸出:錯誤碼:0

$formattedMessage = $formatter->format([
    'name' => 'John',
    'count' => 5
]);
echo $formattedMessage;  // 輸出:Hello, John! You have 5 new messages.

$errorCode = $formatter->getErrorCode();
echo "錯誤碼:" . $errorCode;  // 輸出:錯誤碼:0

注意:getErrorCode() 函式只能在呼叫 format() 函式之後才能獲取到錯誤碼。如果在呼叫 format() 之前呼叫 getErrorCode(),將返回0。如果在呼叫 format() 之後出現錯誤,getErrorCode() 將返回一個非零的錯誤碼。

補充糾錯
熱門PHP函式
分享連結