PHP applications can be difficult to maintain especially when dealing with legacy code. Debugging can be a nuisance. Here are some tips.

* The methods die() and exit() are equivalent en terminate the script on the spot with a message.
* print_r() , var_export() and var_dump() output all information on any variable. Subtle differences: print_r() dumps human-readable output, var_export() dumps or returns valid PHP code for example an array.
* func_get_args() get quick access to all the function arguments at a certain point
* debug_backtrace() for generating the PHP backtrace at a certain point. Set option DEBUG_BACKTRACE_IGNORE_ARGS to skip the arguments collected on the way.
* prettify output with echo “<pre>” . $str . “</pre>”; especially when outputting xml content

Retrieve the name of the file a class was defined in:


$foo = new foo();
$reflector = new ReflectionClass($foo);
echo $reflector->getFileName();

Headers management

* headers_sent() and headers_list() check if and which headers have already been sent.
* ob_end_flush(). Flushes the output buffer. ob_get_contents() returns the content of the output buffer. Handy in the event of those “headers already sent” errors.

Running out of memory

* get_memory_usage() returns the amount of memory in use

Advertisements