Previous Thread
Next Thread
Print Thread
Rate Thread
Joined: Apr 2002
Posts: 1,768
Addict
Addict
Offline
Joined: Apr 2002
Posts: 1,768
The PHP var_dump() function is handy for debugging. In a web script, it's not always practical or desirable to send debug output to the browser, so I spent the time figuring out how to redirect the output to a file. Here's an example:

Code
<?php <br /> <br />echo "testing capture of var_dump output<br />\n"; <br /> <br />// test data <br />$a = array( <br />   'x' => 'x_value', <br />   'y' => 'y_value', <br />   array( <br />      'z' => 'z_value', <br />      'w' => 'z_value' <br />   ), <br />); <br /> <br />// output-file <br />define ('OB_LOG', 'ob_test.log'); <br /> <br />// clear output-file if it exists (optional) <br />if (file_exists(OB_LOG)) { <br />   unlink(OB_LOG); <br />} <br /> <br />// method 1 - output buffer explicitly <br />ob_start(); <br />echo "test 2\n"; <br />echo "a=\n"; <br />var_dump($a); <br />error_log(ob_get_contents(), 3, OB_LOG); <br />ob_end_clean(); <br /> <br />// method 2 - use callback function to output buffer <br />ob_start('callback'); <br />echo "test 1\n"; <br />echo "a=\n"; <br />var_dump($a); <br />ob_end_flush(); <br /> <br />echo "done<br />\n"; <br /> <br />// callback function for ob_start() - only needed if using method 2 <br />function callback($buf) { <br />   error_log($buf, 3, OB_LOG); <br />   return ''; <br />} <br /> <br />?>


Outputting the buffer explicitly (method 1) is simpler, but using a callback function (method 2) might be preferable if you're doing the output in several places.

Last edited by Dave_L; 10/01/2003 5:48 PM.
Sponsored Links

Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
JAISP
JAISP
PA
Posts: 449
Joined: February 2008
Forum Statistics
Forums63
Topics37,573
Posts293,925
Members13,849
Most Online5,166
Sep 15th, 2019
Today's Statistics
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,833
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2024 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.0.0
(Preview build 20221218)