What is the purpose of the optional second parameter in the print_r() function in PHP?

The optional second parameter in the print_r() function in PHP is used to specify whether the output should be returned as a string instead of being directly printed to the output. This can be useful when you need to store the output in a variable for further processing or debugging purposes.

// Example of using the optional second parameter in print_r() function
$array = ['apple', 'banana', 'cherry'];
$output = print_r($array, true); // store the output in a variable
echo $output; // print the output stored in the variable