Content · Programming · PHP

PHP strtr() Function: Usage and Examples

Translates characters or replaces multiple substrings according to a mapping.

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

PHP strtr() translates characters or replaces multiple substrings according to a mapping. The examples below show how the call behaves, where it is useful, and which limitations matter.

What Is strtr() in PHP?


Translates characters or replaces multiple substrings according to a mapping.

Its call structure is:

strtr($string, $from, [$to])

Use either from/to byte translation arguments or an associative array of whole substrings to replace.

Example 1: Basic Usage


This example introduces strtr with a direct call and a small, readable input.

<?php

echo strtr("abc", "abc", "123");

Output:

123

Translates characters or replaces multiple substrings according to a mapping. The two-string and associative-array forms behave differently; replacement maps are not recursive.

Example 2: Another Behavior or Input


Compare this input with the previous example: changing the data or option can affect how strtr behaves.

<?php

echo strtr("Hello USER", ["USER" => "Alex"]);

Output:

Hello Alex

The changed input illustrates why output may differ with the chosen values or environment. The two-string and associative-array forms behave differently; replacement maps are not recursive.

Practical Example: Using the Feature


The following example incorporates strtr into a workflow. Values such as paths, connection handles and configuration options must come from trusted application settings.

<?php

$template = "Hi {name}, order {id} is ready"; echo strtr($template, ["{name}" => "Sam", "{id}" => "A17"]);

Output:

Hi Sam, order A17 is ready

In a real application, check each operation result, validate values before use and clean up acquired resources. The two-string and associative-array forms behave differently; replacement maps are not recursive.

Important Behavior and Edge Cases


  • Purpose: Translates characters or replaces multiple substrings according to a mapping.
  • Key limitation: The two-string and associative-array forms behave differently; replacement maps are not recursive.
  • Failure handling: Check return values and handle invalid inputs and unavailable operating-system or extension features where relevant.

Comparison With Related Functions


Function or ApproachBest Fit
strtr()Translates characters or replaces multiple substrings according to a mapping.
str_replace()Replaces selected substring matches; the array form of strtr() applies a mapping without cascading replacements.

Common Beginner Mistakes


MistakeCorrect Approach
The two-string and associative-array forms behave differently; replacement maps are not recursive.The correct approach depends on this behavior: The two-string and associative-array forms behave differently; replacement maps are not recursive.
Ignoring errors or cleanup after the operation.Validate inputs and check each result; close files, restore settings or release resources when the operation requires it.

Key Takeaways


  • Translates characters or replaces multiple substrings according to a mapping.
  • The two-string and associative-array forms behave differently; replacement maps are not recursive.
  • Use the examples as starting points, and adapt environmental paths, resources and permissions rather than assuming fixed results.

strtr is useful when its specific behavior fits the problem. The safest implementations combine the correct API with input validation, error checking and resource cleanup.

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