When working with PHP, you may need to read the current ICU message pattern. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
MessageFormatter::getPattern: The returned text is a formatting template, not the formatted message.
What Is MessageFormatter::getPattern in PHP?
Read the current ICU message pattern. The returned text is a formatting template, not the formatted message.
Syntax:
$formatter->getPattern(): string|false
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: A pattern can contain braces and ICU syntax; do not treat it as rendered HTML.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
$formatter = new MessageFormatter("en_US", "Hello, {name}!");
echo $formatter->getPattern();
The code focuses on the main operation. The returned text is a formatting template, not the formatted message.
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", "Items: {count, number}");
echo $formatter->getPattern();
What changes? A pattern can contain braces and ICU syntax; do not treat it as rendered HTML.
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
$patterns = ["Hello, {name}!", "Welcome back, {name}!"];
foreach ($patterns as $pattern) {
$formatter = new MessageFormatter("en_US", $pattern);
echo $formatter->getPattern() . PHP_EOL;
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. The returned text is a formatting template, not the formatted message.
Important Behavior and Edge Cases
- Core behavior: The returned text is a formatting template, not the formatted message.
- Inputs and results: A pattern can contain braces and ICU syntax; do not treat it as rendered HTML.
- Alternatives: MessageFormatter::setPattern() changes the pattern used by an existing formatter.
- Compatibility: The intl extension is required.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
MessageFormatter::getPattern | Read the current icu message pattern. |
| Related PHP approach | MessageFormatter::setPattern() changes the pattern used by an existing formatter. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | A pattern can contain braces and ICU syntax; do not treat it as rendered HTML. |
| Confusing this API with a related operation | MessageFormatter::setPattern() changes the pattern used by an existing formatter. |
| Assuming output is identical across environments | Check return values and required extensions or versions. The intl extension is required. |
Key Takeaways
- MessageFormatter::getPattern helps you read the current ICU message pattern.
- The returned text is a formatting template, not the formatted message.
- A pattern can contain braces and ICU syntax; do not treat it as rendered HTML.
Use MessageFormatter::getPattern when you need to read the current ICU message pattern. 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.