Skip to content
PHP Logo

In what situations is it more efficient to use implode() instead of print_r for array manipulation in PHP?

When you need to convert an array into a string, it is more efficient to use implode() instead of print_r(). Print_r() is primarily used for debugging purposes and outputs the array in a human-readable format, while implode() allows you to specify a delimiter to join the array elements into a single string.

php
$array = array('apple', 'banana', 'orange');
$string = implode(', ', $array);
echo $string;