Actions

Difference between revisions of "How to debug your code"

From Joomla! Documentation

(Converted bold title to section headings. Added a section for the JFirePHP extension. Corrected grammar.)
Line 1: Line 1:
'''The Easy Way:'''
+
==The Easy Way==
 
The simplest way to see what is going on inside your code is to temporarily add echo statements for variables to show their values on the screen. For example, say you want to know what the value of some variables are when $i is "5". You could use code like this:
 
The simplest way to see what is going on inside your code is to temporarily add echo statements for variables to show their values on the screen. For example, say you want to know what the value of some variables are when $i is "5". You could use code like this:
  
Line 10: Line 10:
  
 
This works for simple situations. However, if you are planning on doing a lot of Joomla! development, it is worth the effort to install and learn an integrated development environment (IDE) that includes a real PHP debugger.
 
This works for simple situations. However, if you are planning on doing a lot of Joomla! development, it is worth the effort to install and learn an integrated development environment (IDE) that includes a real PHP debugger.
 
+
==Using an IDE==
'''Using an IDE:'''
+
 
Many Joomla! developers use the Eclipse IDE. This is free and includes a debugger. Instructions for installing it are available at [[Setting_up_your_workstation_for_Joomla!_development]].
 
Many Joomla! developers use the Eclipse IDE. This is free and includes a debugger. Instructions for installing it are available at [[Setting_up_your_workstation_for_Joomla!_development]].
 
+
==Using the PHP Expert editor==
'''Using the PHP Expert editor:'''
+
 
Another option is the PHP Expert editor with an installed extension for debugging. Add the following lines to the php.ini file:
 
Another option is the PHP Expert editor with an installed extension for debugging. Add the following lines to the php.ini file:
  
Line 22: Line 20:
 
  debugger.profiler_enabled=off
 
  debugger.profiler_enabled=off
  
It is best to set profiler_enable to "off". Then you need set options in the menu Run/Options menu to use HTTP-server and the directory to where your script is located.
+
It is best to set profiler_enable to "off". Then you need to set options in the Run/Options menu to use HTTP-server and the directory in which your script is located.
 
+
If all options are correct you may run your script in debug mode by clicking on the Debug button (F8).
+
 
+
'''J!Dump:'''
+
An often handy extension that can be found in the JED is the J!Dump extension that will allow you to dump variable, stack traces, and system information into a popup window at run time. This extension works similarly to the php command `var_dump` but formats the output in a much more readable fashion.
+
  
'''JDEBUG'''
+
If all options are correct, you may run your script in debug mode by clicking on the Debug button (F8)
To check if the website is in debug-mode, you test the JDEBUG - variable:  
+
==J!Dump==
 +
An often handy extension that can be found in the JED is the J!Dump extension that will allow you to dump variable, stack traces, and system information into a popup window at run time. This extension works like the PHP command `var_dump` but formats the output in a much more readable fashion.
 +
==JDEBUG==
 +
To check whether the Website is in the debug mode, test the JDEBUG variable:  
 
<source lang="PHP">
 
<source lang="PHP">
 
if(JDEBUG){
 
if(JDEBUG){
Line 36: Line 32:
 
}
 
}
 
</source>
 
</source>
 +
==JFirePHP==
 +
Use the Joomla [http://extensions.joomla.org/extensions/miscellaneous/development/11343 JFirePHP extension].
  
 
 
[[Category:Development]]
 
[[Category:Development]]

Revision as of 17:29, 27 April 2012

Contents

The Easy Way

The simplest way to see what is going on inside your code is to temporarily add echo statements for variables to show their values on the screen. For example, say you want to know what the value of some variables are when $i is "5". You could use code like this:

for ( $i = 0; $i < 10; $i++ ) {
        if ( $i == 5 ) {
                echo '$i=' . $i; 
                // other echo statements 
        }
}

This works for simple situations. However, if you are planning on doing a lot of Joomla! development, it is worth the effort to install and learn an integrated development environment (IDE) that includes a real PHP debugger.

Using an IDE

Many Joomla! developers use the Eclipse IDE. This is free and includes a debugger. Instructions for installing it are available at Setting_up_your_workstation_for_Joomla!_development.

Using the PHP Expert editor

Another option is the PHP Expert editor with an installed extension for debugging. Add the following lines to the php.ini file:

extension=php_dbg.dll
[Debugger]
debugger.enabled=on
debugger.profiler_enabled=off

It is best to set profiler_enable to "off". Then you need to set options in the Run/Options menu to use HTTP-server and the directory in which your script is located.

If all options are correct, you may run your script in debug mode by clicking on the Debug button (F8)

J!Dump

An often handy extension that can be found in the JED is the J!Dump extension that will allow you to dump variable, stack traces, and system information into a popup window at run time. This extension works like the PHP command `var_dump` but formats the output in a much more readable fashion.

JDEBUG

To check whether the Website is in the debug mode, test the JDEBUG variable:

if(JDEBUG){
//whatever debugging code you want to run
}

JFirePHP

Use the Joomla JFirePHP extension.