Actions

Difference between revisions of "How to debug your code"

From Joomla! Documentation

m
(edited to correct grammar and added link to eclipse installation instructions)
Line 1: Line 1:
The simplest way for debug your code is create echo items for variables and output it into the screen. For expample, you want to now what value of the index item in for cycle:
+
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++ ) {
+
<source lang="PHP">for ( $i = 0; $i < 10; $i++ ) {
 
if ( $i == 5 ) {
 
if ( $i == 5 ) {
echo $i;
+
echo "$i=" . $i;  
 +
                // other echo statements
 
}
 
}
}
+
}</source>
  
But this way is to hard for debug real code with more than 1000 lines of code. For this perpose better use debuggers plugged into the editors. For example it is PHP Expert editor with installed extension
+
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.
  
add lines to the php.ini file:
+
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]].
  
extension=php_dbg.dll
+
Another option is the PHP Expert editor with an installed extension for debugging. Add the following lines to the php.ini file:
[Debugger]
+
debugger.enabled=on
+
debugger.profiler_enabled=off
+
  
profiler_enable better set to off, it is very deceleration option
+
extension=php_dbg.dll
 +
[Debugger]
 +
debugger.enabled=on
 +
debugger.profiler_enabled=off
  
 +
It is best to set profiler_enable to "off". Than you need set options in the menu Run/Options menu to use HTTP-server and the directory to where your script is located.
  
Than you need set options in menu Run/Options menu to use HTTP-server and directory where your script located.
+
If all options are correct you may run you script in debug mode by clicking on the Debug button (F8).
 
+
If all options are correct you may run you script in debug mode by clicking on the Debug button (F8)
+
  
 
[[Category:Development]]
 
[[Category:Development]]

Revision as of 10:41, 9 September 2008

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.

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.

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". Than you need set options in the menu Run/Options menu to use HTTP-server and the directory to where your script is located.

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