I'm having a hard time understanding when
strtr would be preferable to
str_replace or vice versa. It seems that it's possible to achieve the exact same results using either function, although the order in which substrings are replaced is reversed. For example:
echo strtr('test string', 'st', 'XY')."n"; echo strtr('test string', array( 's' => 'X', 't' => 'Y', 'st' => 'Z' ))."n"; echo str_replace(array('s', 't', 'st'), array('X', 'Y', 'Z'), 'test string')."n"; echo str_replace(array('st', 't', 's'), array('Z', 'Y', 'X'), 'test string');
YeXY XYring YeZ Zring YeXY XYring YeZ Zring
Aside from syntax, is there any benefit to using one over the other? Any cases where one would not be sufficient to achieve a desired result?