When debugging a php application, print_r() or var_dump() often use to display the data inside the variable, usually an array that store many data inside. print_r() is core php function to displays information about a variable in a way that’s readable by humans. Not just a variable or an array, we can trace or display protected and private properties of objects in PHP 5.
But when the php application is called by the curl or other remote connection (not using a web browser), print_r() won’t show on the client as return as it won’t write as output. So to debug a remote application, i often write the result or output from print_r() to a debug file.
To write print_r() result we need to use output buffering, see the example:
$data = array('one', 'two', 'three'); ob_start(); //Start buffering print_r($data); //print the result $output = ob_get_contents(); //get the result from buffer ob_end_clean(); //close buffer $h = fopen('log.txt', 'w'); //open a file fwrite($h, $output); //write the output text fclose($h); //close file
This is a simple trick to write the output of print_r() to the file. If this what you looking for, leave any comment or at least say thanks to support me.