Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am newbie on PHP. Here is my situation. I write my code in vim and put it under /var/www/ then I can use

localhost/*.php

to run my code on browser.

When my code has bug. It just come out nothing.

How can I debug mu code like c++ or java?

Thanks.

Edited:

The link some friends provide is not helpful for me. I am under Linux. That's for Win.

share|improve this question
4  
using search is useful: stackoverflow.com/questions/5243572/how-to-debug-php-code –  AlexAtNet Apr 19 '11 at 1:40
    
possible duplicate of How do you debug PHP scripts? –  tobyodavies Apr 19 '11 at 1:47
    
which is already a duplicate lol –  Spyros Apr 19 '11 at 1:47
    
This link is not helpful for me. I am under Linux. That's for Win. –  Don Lun Apr 19 '11 at 1:57

7 Answers 7

up vote 3 down vote accepted

If on a localhost, I would suggest using firefox or chrome and installing firebug for mozilla, and chrome gets a default. Be sure that on a local host your settings are matched to the server you are uploading to as this can cause problems when going live.

Specifically most shared hosting has PHP on safe mode and output buffering off, so if you use it, use it by calling it by calling ob_start(); etc, otherwise you should have no problems, and learning to debug is part of the fun, helps you learn alot :)

As for php errors just re-edit your php.ini file, you can find al relevant information on http://php.net

Happy Coding

share|improve this answer

For more advanced solution, you can use XDebug extension 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.
share|improve this answer

You can use error_reporting() at the top of your code...

error_reporting(E_ALL);

You will also want display_errors on in php.ini.

Note that you should have public facing error reporting off in a production environment.

share|improve this answer

Though i personally find var_dump just enough for my php debugging, some people tend to like using debuggers like xdebug to do so.

share|improve this answer

You can check the log file output by PHP. A good way to see your configuration is to use phpinfo().

Also you can set error_reporting() so you can see the error message instead of a white page.

share|improve this answer

PHP produces an error_log file in its directory whenever a problem occurs, you can find debug information there.

Also, try using var_dump($someVarible). This will give you useful information about the current state of a variable - often better than echo.

share|improve this answer
    
This all depends on your settings, he should first compare his virtual to his live server to ensure all similaritys are in place to avoid confusion when going live. –  Version1 Apr 19 '11 at 1:44
    
Well yes, but by default this will be true. –  Nick Brunt Apr 19 '11 at 1:49
    
That depends on what virtual server he is using, and if he has set up a mirror to replicate his actual hosting it may not be so. Best to make sure, too many times I have created on localhost, and found many problems when going live. –  Version1 Apr 19 '11 at 1:54

In most cases, if you set you error_reportto -1 you'll be able to see all notices, warning and errors in your browser.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.