Difference between revisions of "How to debug your code"

From Joomla! Documentation

m
(10 intermediate revisions by 7 users not shown)
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 Easy Way 1 (echo)==
 +
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>
 +
 
 +
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.
  
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
+
==The Easy Way 2 (joomla message)==
 +
Your code won't always display simple echo statements. In that case you can try this alternative, still easy way:
 +
<source lang="PHP">JFactory::getApplication()->enqueueMessage( 'Some debug string(s)');</source>
 +
You can choose different [[Display_error_messages_and_notices|message types]] which corresponds to grouping with different styles (colors mainly).
  
add lines to the php.ini file:
+
==Using an IDE==
 +
Check this 3 minutes video that shows [http://www.youtube.com/watch?v=sP4dHAuq2kc&feature=youtu.be how you can debug your code with a Browser and an IDE]
  
extension=php_dbg.dll
+
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]].
[Debugger]
 
debugger.enabled=on
 
debugger.profiler_enabled=off
 
  
profiler_enable better set to off, it is very deceleration option
+
==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
  
Than you need set options in menu Run/Options menu to use HTTP-server and directory where your script 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 you script in debug mode by clicking on the Debug button (F8)
+
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:
 +
<source lang="PHP">
 +
if(JDEBUG){
 +
//whatever debugging code you want to run
 +
}
 +
</source>
 +
==JFirePHP==
 +
Use the Joomla [http://extensions.joomla.org/extensions/miscellaneous/development/11343 JFirePHP extension].
  
 
[[Category:Development]]
 
[[Category:Development]]

Revision as of 05:48, 6 March 2013

The Easy Way 1 (echo)[edit]

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.

The Easy Way 2 (joomla message)[edit]

Your code won't always display simple echo statements. In that case you can try this alternative, still easy way:

JFactory::getApplication()->enqueueMessage( 'Some debug string(s)');

You can choose different message types which corresponds to grouping with different styles (colors mainly).

Using an IDE[edit]

Check this 3 minutes video that shows how you can debug your code with a Browser and 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[edit]

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[edit]

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[edit]

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

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

JFirePHP[edit]

Use the Joomla JFirePHP extension.