When working with PHP, you may need to format a message without storing a formatter object. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
MessageFormatter::formatMessage: This static shortcut builds the formatter internally for one call.
What Is MessageFormatter::formatMessage in PHP?
Format a message without storing a formatter object. This static shortcut builds the formatter internally for one call.
Syntax:
MessageFormatter::formatMessage(string $locale, string $pattern, array $values): 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: Patterns are ICU MessageFormat patterns, not printf or sprintf format strings.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
echo MessageFormatter::formatMessage("en_US", "Hello, {name}!", ["name" => "Alex"]);
The code focuses on the main operation. This static shortcut builds the formatter internally for one call.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
echo MessageFormatter::formatMessage("en_US", "Count: {count, number}", ["count" => 42]);
What changes? Patterns are ICU MessageFormat patterns, not printf or sprintf format strings.
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
$messages = ["Ana" => 2, "Sam" => 5];
foreach ($messages as $person => $quantity) {
echo MessageFormatter::formatMessage("en_US", "{person}: {quantity, number} items", ["person" => $person, "quantity" => $quantity]) . PHP_EOL;
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. This static shortcut builds the formatter internally for one call.
Important Behavior and Edge Cases
- Core behavior: This static shortcut builds the formatter internally for one call.
- Inputs and results: Patterns are ICU MessageFormat patterns, not printf or sprintf format strings.
- Alternatives: MessageFormatter::create() and format() can reuse a compiled pattern.
- Compatibility: The intl extension is required.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
MessageFormatter::formatMessage | Format a message without storing a formatter object. |
| Related PHP approach | MessageFormatter::create() and format() can reuse a compiled pattern. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Patterns are ICU MessageFormat patterns, not printf or sprintf format strings. |
| Confusing this API with a related operation | MessageFormatter::create() and format() can reuse a compiled pattern. |
| Assuming output is identical across environments | Check return values and required extensions or versions. The intl extension is required. |
Key Takeaways
- MessageFormatter::formatMessage helps you format a message without storing a formatter object.
- This static shortcut builds the formatter internally for one call.
- Patterns are ICU MessageFormat patterns, not printf or sprintf format strings.
Use MessageFormatter::formatMessage when you need to format a message without storing a formatter 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.