There are several ways you can debug your php code depending on the issues you are having. Most of the methods below will display a string, so you also need to know how to convert arrays and objects to strings in order to display them.
Converting other types of data to strings
Converting an array to a string
PHP:
$string = json_encode($array);
Converting an object to a string
Assumiong that your object implements a __toString() function:
PHP:
$string = (string) $object;
PHP echo
The most obvious way to output debugging information is to use the php "echo" statement. However, this is not always as straightforward as you might think because the output will often appear somewhere in your HTML which may not be immediately obvious or even visible, but can sometimes be missing altogether if you outputted it during a php output buffer capture ("ob_start"):
PHP:
echo "$string = " . $string;
You may need to use your browser's Developer's Tools to search for the output.
You also need to be aware that if page headers are still being created, your output will terminate these and may create other output oddities.
Joomla's standard message area
Possibly the easiest way to put a debug message on screen is to send it to Joomla's standard message area:
PHP:
JFactory::getApplication()->enqueueMessage($string, 'message');
Fabrik's debugging infrastructure
If you want to leave debugging code permanently in your php, then you can use Fabrik's own debugging system.
Turning on Fabrik's debugging system
To view additional debug output showing what Fabrik is doing, you need to do two things:
- Set 'allow fabrik debug' to either 'Yes' or 'Debug JS' in Fabrik's global parameters.
- Add to the end of the URL the following string:
or
if there are no query parameters in the URL already.
Permanent debugging statements
To display your own debugging information alongside Fabrik's, use:
PHP:
FabrikHelperHTML::debug($string, 'debug:heading');
Browser Developer's Tools
To get debugging information when the output is being sent in response to an Ajax call for e.g. field or form
Validation or for calc
Elements, and is not immediately visible the browser window.
You can use the following methods :
Use your browser's Developer's Tools
To display variables, you can use the PHP var_dump() function :
PHP:
var_dump("MY_VAR : ",$my_var);
exit;
When the , this will stop the execution of the plugin and you will get the following error on your form :
SyntaxError: JSON.parse: unexpected character
For example in Firefox's Developer's Tools / Firebug, in the
Javascript Console you will see a POST with:
SyntaxError: JSON.parse: unexpected character
Click on the '+' symbol before "POST". You will see a new menu, by default on "Response" tab. Click on "HTML" tab, you will see your dump in a more visible way.
If you want to display many variables, you can write :
PHP:
echo "found : " . $found . "\n";
echo "status : " . $status; exit;
The "\n" is to display the next variable on a new line.
And don't forget to write "exit;" on the last line !
http://screencast.com/t/1lPY94LRQdsD
Joomla JDump extension
JDump allows you to display your debugging output in a popup window, without stopping the execution of your code. Just insert in your code something like:
PHP:
dump($variable, 'Variable Name');
In case if you get the following error at the backend of Joomla after installing JDump:
Code:
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; plgSystemDump has a deprecated constructor in /home/
Replace this code:
Code:
function plgSystemDump(& $subject, $params) {
parent::__construct($subject, $params);
with
Code:
function __construct(& $subject, $params) {
parent::__construct($subject, $params);
in
/plugins/system/dump/dump.php on lines
27 / 28
See also...
https://docs.joomla.org/How_to_debug_your_code
https://www.joomlabeginner.com/blog/tutorials/59-how-to-use-joomla-debug-system