TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-06-2007, 03:16 PM   #1 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 438
Thanks: 22
Karl is on a distinguished road
Default Tip: error_log and arrays

Output buffering can be used to capture different types of output. For example, lets say your script needs to log an array, we could achieve this like:

PHP Code:
ob_start();
print_r($szArray);
$szLog ob_get_clean();

error_log($szLog); 
What this essentially does is output the array, but becuase we have output buffering on we can catch the output and store it in a varialbe ($szLog) which we can then log to a file (or other source) via the error_log function.

Hope this tip helps someone, it can be very useful for cron type scripts.

Last edited by Karl : 09-06-2007 at 04:09 PM.
Karl is offline  
Reply With Quote
Old 09-06-2007, 03:46 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 713
Thanks: 2
Salathe is on a distinguished road
Default

As of PHP 4.3.0, there is an optional second parameter to the print_r function which is a boolean dictating whether the output from print_r is actually output (default) or returned as a string.

Your example can be therefore slightly altered to something more like:
PHP Code:
$aArray = array('one''two''three''four');
// Return print_r output, log to custom log file debug.log
error_log(print_r($aArraytrue), 3'debug.log'); 
Salathe is offline  
Reply With Quote
Old 09-06-2007, 03:56 PM   #3 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 438
Thanks: 22
Karl is on a distinguished road
Default

well there you go, all that time and I never knew print_r had a second argument.

Im gunna have to update the tip now :)
Karl is offline  
Reply With Quote
Old 09-06-2007, 04:02 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 713
Thanks: 2
Salathe is on a distinguished road
Default

No problem, we all oftentimes just stick with what we've been doing from years and years ago. The introduction of little changes (like the inclusion of another argument) is very easy to miss, especially if the 'old' way just works fine also.

P.S. Sorry for coming in and making you edit these tips :p
Salathe is offline  
Reply With Quote
Old 09-06-2007, 04:07 PM   #5 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 438
Thanks: 22
Karl is on a distinguished road
Default

Lol np, I'm glad you did, I'll certainly be using it from now on! I just wish I didn't edit it now tbh, as it has ruined the flow of the thread. I may have to re-edit it back to how it originally was then post the updated tip below :)
Karl is offline  
Reply With Quote
Old 09-06-2007, 04:11 PM   #6 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 438
Thanks: 22
Karl is on a distinguished road
Default

Here's an updated example that shows how it can be used for outputting the debug backtrace to an error log.

PHP Code:
ob_start();
debug_print_backtrace();
$szLog ob_get_clean();

error_log($szLog); 
Karl is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:45 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0