From PHP Docs to str_replace :
// Outputs F because A is replaced with B, then B is replaced with C, and so on... // Finally E is replaced with F, because of left to right replacements. $search = array('A', 'B', 'C', 'D', 'E'); $replace = array('B', 'C', 'D', 'E', 'F'); $subject = 'A'; echo str_replace($search, $replace, $subject); // Outputs: apearpearle pear // For the same reason mentioned above $letters = array('a', 'p'); $fruit = array('apple', 'pear'); $text = 'a p'; $output = str_replace($letters, $fruit, $text); echo $output;
Looking at these examples, PHP uses str_replace for each $search node array, so both of your examples are the same in terms of performance, however, you are sure that using the array to search and replace is more readable and future -prof, since you can easily change the array in future.
user529649
source share