var_log() Archives - Justin Silver https://www.justinsilver.com/tag/var_log/ Technology, Travel, and Pictures Wed, 20 Apr 2016 01:34:48 +0000 en-US hourly 1 https://wordpress.org/?v=6.0.1 https://www.justinsilver.com/wp-content/uploads/2013/06/cropped-apple-touch-icon-160x160.png var_log() Archives - Justin Silver https://www.justinsilver.com/tag/var_log/ 32 32 Writing to the PHP error_log with var_dump & print_r https://www.justinsilver.com/technology/writing-to-the-php-error_log-with-var_dump-and-print_r/?utm_source=rss&utm_medium=rss&utm_campaign=writing-to-the-php-error_log-with-var_dump-and-print_r https://www.justinsilver.com/technology/writing-to-the-php-error_log-with-var_dump-and-print_r/#comments Thu, 01 Nov 2012 17:08:43 +0000 http://justin.ag/?p=2697 Writing objects to the PHP error log using error_log() can be useful, but it isn’t allowed – you can only write strings there. What to do? Well, luckily there are options for both print_r()...

The post Writing to the PHP error_log with var_dump & print_r appeared first on Justin Silver.

]]>
AmpedSense.OptimizeAdSpot('AP'); AmpedSense.OptimizeAdSpot('IL'); AmpedSense.OptimizeAdSpot('IR');

Writing objects to the PHP error log using error_log() can be useful, but it isn’t allowed – you can only write strings there. What to do?

Well, luckily there are options for both print_r() – human friendly – and var_dump() – parseable – that will let you convert an object into a string.

Use print_r()

Using print_r() is the easiest, since the second parameter to the function says that we want to send the output to a variable rather than echoing it. Thus, to write to the error_log you just need to use the following code.

Use var_dump()

If you want the more detailed output of var_dump(), it’s a bit trickier, but still pretty easy. In a nutshell, since you can only echo the results you have to capture the output buffer with ob_start(), assign it to a variable, and then clean the buffer with ob_end_clean() allowing you to write the resulting variable to the error_log.

function var_error_log( $object=null ){
    ob_start();                    // start buffer capture
    var_dump( $object );           // dump the values
    $contents = ob_get_contents(); // put the buffer into a variable
    ob_end_clean();                // end capture
    error_log( $contents );        // log contents of the result of var_dump( $object )
}

$object = new MyObject();
var_error_log( $object );

The post Writing to the PHP error_log with var_dump & print_r appeared first on Justin Silver.

]]>
https://www.justinsilver.com/technology/writing-to-the-php-error_log-with-var_dump-and-print_r/feed/ 13