How to debug a PHP script

It could be useful sometimes to know what variables are defined at particular point of PHP script in order to have a better understanding of the code you are debugging. In contrast with C++, PHP has a magic function get_defined_vars ( void ) that  returns an array of all defined variables with their values so we can output all variable names with the following line of code:

print_r(array_keys(get_defined_vars()));

To know where are this variables come from we can print the stack trace:

debug_print_backtrace();

And, of course we can examine log files located on the web server in /var/log/nginx/ or /var/log/apache/ directories.

Leave a Reply

Your email address will not be published. Required fields are marked *