English | 简体中文 | 繁體中文
查询

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函数
分享链接