Use gettext() when you need to looks up a translated message using the active gettext domain in a PHP application. The examples below show the actual operation and its limitations.
What Is gettext() in PHP?
Looks up a translated message using the active gettext domain. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
gettext(string $message): string
Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.
Example 1: Basic Usage
<?php
echo gettext("Welcome");
Output:
Welcome
Example 2: Different Input or Behavior
<?php
echo gettext("Settings");
Output:
Settings
Practical Example: Applying the Function
<?php
$labels = ["Home","Search","Help"]; foreach ($labels as $label) echo gettext($label) . PHP_EOL;
Output:
Home
Search
Help
Important Behavior and Edge Cases
Without locale catalogs, the original string is generally returned; translation is configuration-dependent.
Comparison With Related Functions
| Approach | Difference |
|---|---|
gettext() | Looks up a translated message using the active gettext domain. |
| Related operation | _() is an alias for gettext(). |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Without locale catalogs, the original string is generally returned; translation is configuration-dependent. |
| Confusing this function with a related operation | _() is an alias for gettext(). |
Key Takeaways
- Looks up a translated message using the active gettext domain.
- Without locale catalogs, the original string is generally returned; translation is configuration-dependent.
- _() is an alias for gettext().
Use gettext() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.