Artemix
Artemix

Reputation: 8655

I need to debug PHP. What is my best choice?

I'm working on a web site under a Linux environment (Debian). I'm not an expert at Linux but I can handle it, and the website is made using PHP, MySQL, HTML, etc.

The thing is, I'm using PHP for the server side. Right now, for testing, I installed Apache on my PC so I can test everything. But, it would be great if I could debug the PHP code. So far, I didn't need it, but now the code is getting larger, it's a must.

So far, I'm using vim and everything is fine, but, how can I debug PHP in my case? What tools should I install? Are they free?

Basically, I need to know what would be the best choice in my situation.

Upvotes: 14

Views: 24300

Answers (5)

sameers
sameers

Reputation: 5095

Specifically, if you're (a newbie like me that found this SO thread after starting out with PHP for the first time and are) trying to figure out what generates the error message in the first place, start with error_log(). That's the function that sends a string as "an error message to the web server's error log or to a file."

This is the PHP SAPI standard error handling (which is logging) and configured by default.

In general, to learn the fundamentals of error reporting and configuration in PHP, start with the functions in the Error Handling Functions documentation on PHP.net - the comment on that page by petrov dot michael () gmail com is a helpful place to start.

To actually generate the error, your best option as a newbie (meaning, least amount of documentation to read) is to start with print_r. Setting the second argument of print_r to TRUE will return a string, that you can then pass to error_log.

error_log('what gives $blue: ' . print_r($blue, true));

This is probably the route that will give you the fastest way to debug, given that your PHP code might be sitting inside some framework that adds in various output control layers (like Wordpress.)

Upvotes: 1

kenorb
kenorb

Reputation: 166379

You can install PHP IDE with debugging facilities. It will help you to debug your PHP code step-by-step.

Few popular which has this feature:


For more advanced solution, you can install XDebug extension manually for PHP.

By default when XDebug is loaded, it should show you automatically the backtrace in case of any fatal error. Or you trace into file (xdebug.auto_trace) to have a very big backtrace of the whole request or do the profiling (xdebug.profiler_enable) or other settings. If the trace file is too big, you can use xdebug_start_trace() and xdebug_stop_trace() to dump the partial trace.

Installation

Using PECL:

pecl install xdebug

On Linux:

sudo apt-get install php5-xdebug

On Mac (with Homebrew):

brew tap josegonzalez/php
brew search xdebug
php53-xdebug

Example of mine configuration:

[xdebug]

; Extensions
extension=xdebug.so
; zend_extension="/YOUR_PATH/php/extensions/no-debug-non-zts-20090626/xdebug.so"
; zend_extension="/Applications/MAMP/bin/php/php5.3.20/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so" ; MAMP

; Data
xdebug.show_exception_trace=1       ; bool: Show a stack trace whenever an exception is raised.
xdebug.collect_vars = 1             ; bool: Gather information about which variables are used in a certain scope.
xdebug.show_local_vars=1            ; int: Generate stack dumps in error situations.
xdebug.collect_assignments=1        ; bool: Controls whether Xdebug should add variable assignments to function traces.
xdebug.collect_params=4             ; int1-4: Collect the parameters passed to functions when a function call is recorded.
xdebug.collect_return=1             ; bool: Write the return value of function calls to the trace files.
xdebug.var_display_max_children=256 ; int: Amount of array children and object's properties are shown.
xdebug.var_display_max_data=1024    ; int: Max string length that is shown when variables are displayed.
xdebug.var_display_max_depth=3      ; int: How many nested levels of array/object elements are displayed.
xdebug.show_mem_delta=0             ; int: Show the difference in memory usage between function calls.

; Trace
xdebug.auto_trace=0                 ; bool: The tracing of function calls will be enabled just before the script is run.
xdebug.trace_output_dir="/var/log/xdebug" ; string: Directory where the tracing files will be written to.
xdebug.trace_output_name="%H%R-%s-%t"     ; string: Name of the file that is used to dump traces into.

; Profiler
xdebug.profiler_enable=0            ; bool: Profiler which creates files read by KCacheGrind.
xdebug.profiler_output_dir="/var/log/xdebug"  ; string: Directory where the profiler output will be written to.
xdebug.profiler_output_name="%H%R-%s-%t"      ; string: Name of the file that is used to dump traces into.
xdebug.profiler_append=0            ; bool: Files will not be overwritten when a new request would map to the same file.

; CLI
xdebug.cli_color=1                  ; bool: Color var_dumps and stack traces output when in CLI mode.

; Remote debugging
xdebug.remote_enable=off            ; bool: Try to contact a debug client which is listening on the host and port.
xdebug.remote_autostart=off         ; bool: Start a remote debugging session even GET/POST/COOKIE variable is not present.
xdebug.remote_handler=dbgp          ; select: php3/gdb/dbgp: The DBGp protocol is the only supported protocol.
xdebug.remote_host=localhost        ; string: Host/ip where the debug client is running.
xdebug.remote_port=9000             ; integer: The port to which Xdebug tries to connect on the remote host.
xdebug.remote_mode=req              ; select(req,jit): Selects when a debug connection is initiated.
xdebug.idekey="xdebug-cli"          ; string: IDE Key Xdebug which should pass on to the DBGp debugger handler.
xdebug.remote_log="/var/log/xdebug.log" ; string: Filename to a file to which all remote debugger communications are logged.

Upvotes: 2

Tekz
Tekz

Reputation: 1309

If your PC OS is Windows then easiest way is to use CodeLobster free edition with any web development stack (WAMP, XAMPP) I found that setting up CodeLobster for debugging is very easy (compared to eclipse or netbeans) and CodeLobster installation is very small compared to other IDEs as well, moreover its free version support full debugging functionality.

You can find detailed step by step guide onhow to setup it in following post How to debug PHP - Easy way

Upvotes: 0

Bharat Khatri
Bharat Khatri

Reputation: 1407

If you're using apache as your web server, then you could use the apache logs to view any errors preventing the successful execution of a PHP script.

You could use

tail -f /var/log/apache2/error.log

to view the apache logs and that would do the job (atleast for a subset of PHP related errors).

Upvotes: 0

greg0ire
greg0ire

Reputation: 23255

XDebug provides step-by-step debugging, and can be used with eclipse PDT, netbeans and even vim. You really should give it a try. There also is Zend Debugger.

Upvotes: 6

Related Questions