Content · Programming · PHP

PHP MessageFormatter::setPattern() Method: Usage and Examples

Learn how to update the ICU formatting pattern on an existing object in PHP with syntax, clear code examples, practical use, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When working with PHP, you may need to update the ICU formatting pattern on an existing object. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.

MessageFormatter::setPattern: This replaces the template while retaining the same formatter locale.

What Is MessageFormatter::setPattern in PHP?

Update the ICU formatting pattern on an existing object. This replaces the template while retaining the same formatter locale.

Syntax:

$formatter->setPattern(string $pattern): bool

Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.

Important: An invalid pattern causes failure; check setPattern() instead of assuming the new template took effect.

Example 1: Basic Usage

This example introduces the operation with a short and predictable input.

<?php

$formatter = new MessageFormatter("en_US", "Hello, {name}!");
$formatter->setPattern("Goodbye, {name}!");
echo $formatter->format(["name" => "Alex"]);

The code focuses on the main operation. This replaces the template while retaining the same formatter locale.

Example 2: Another Case

Now compare a different input, locale, date, or option to see another part of the behavior.

<?php

$formatter = new MessageFormatter("en_US", "Total: {amount, number}");
var_dump($formatter->setPattern("Amount: {amount, number}"));

What changes? An invalid pattern causes failure; check setPattern() instead of assuming the new template took effect.

Practical Example: Applying It in an Application

Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.

<?php

$formatter = new MessageFormatter("en_US", "Hi, {name}!");
$templates = ["Hi, {name}!", "Welcome, {name}!"];
foreach ($templates as $template) {
    if ($formatter->setPattern($template)) {
        echo $formatter->format(["name" => "Maya"]) . PHP_EOL;
    }
}

The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. This replaces the template while retaining the same formatter locale.

Important Behavior and Edge Cases

  • Core behavior: This replaces the template while retaining the same formatter locale.
  • Inputs and results: An invalid pattern causes failure; check setPattern() instead of assuming the new template took effect.
  • Alternatives: MessageFormatter::getPattern() retrieves the stored pattern.
  • Compatibility: The intl extension is required.

Comparison With Related Functions

Function or ApproachWhen to Use It
MessageFormatter::setPatternUpdate the icu formatting pattern on an existing object.
Related PHP approachMessageFormatter::getPattern() retrieves the stored pattern.

Common Beginner Mistakes

MistakeCorrect Approach
Using an incorrect input or optionAn invalid pattern causes failure; check setPattern() instead of assuming the new template took effect.
Confusing this API with a related operationMessageFormatter::getPattern() retrieves the stored pattern.
Assuming output is identical across environmentsCheck return values and required extensions or versions. The intl extension is required.

Key Takeaways

  • MessageFormatter::setPattern helps you update the ICU formatting pattern on an existing object.
  • This replaces the template while retaining the same formatter locale.
  • An invalid pattern causes failure; check setPattern() instead of assuming the new template took effect.

Use MessageFormatter::setPattern when you need to update the ICU formatting pattern on an existing object. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.

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