How to trace PHP code?

I’m trying to figure out what some PHP code is doing. In particular, I need to see inside the structure of some of these PHP object variable. I’m doing


print_r $someStuff;

but it blocks program flow when it displays things to the page. And I see it display “Array()” on a white page. What does that mean? An empty array?

How I can do a print_r to a log file?

Or is there a better way to examine what goes on in PHP code?

Use an editor or IDE that has PHP debugging capabilities.
I use Komodo IDE with xdebug.

Except, I’m debugging code on the server (test application on a test server on a shared webhost). I don’t have a local PHP development environment. I have to use things like debug_backtrace(), var_dump, etc. But they all seem to print to page.

Use remote debugging?
http://xdebug.org/docs/remote

I use the following lines which explode out the array and show you what is contains in a easy to read format ( well to me)

echo '<pre>';
print_r($_POST);
echo '</pre>';

just insert, and doesnt seem to stop code running either.

hope it helps

http://www.php.net/manual/en/function.print-r.php
“If you would like to capture the output of print_r(), use the return parameter.”
After which you could dump it to a log file.

But do yourself a favor and setup a local development machine.

What I have learned…

a) Set up a local dev environment with Komodo IDE and xdebug.

b) But in the mean time, I can do …


$debugsomeStuff = print_r($someStuff,true);
error_log($debugsomeStuff, 0);

and the stuff will show up in my error_log at the root of my application.

Thanks everyone.

Path to error_log is based on server and ini settings.

Here is what I use:


@ini_set('log_errors', 'On');
@ini_set('display_errors', 'Off');
@ini_set('error_log', LOGS_DIR . '/error_php_' . date('Ymd') . '.log');

as you can see, I use ini_set to set path and file name of error log. In my default setting errors goes to Apache error log.

What file to you put those @ini_set?

In php.ini?

No, those function calls would go in your PHP script, or entry-point for your application. :wink: