How to debug PHP codes in DirectPHP
Written by kksou
Sunday, 14 October 2012
If you are using DirectPHP, you'll know that it's very difficult to debug codes in DirectPHP.
This is because DirectPHP uses the standard PHP
eval() function to process your PHP codes. If you have used the
eval() function before, you'll know that it doesn't give you any meaningful error messages. The most you see is something like:
Warning: Invalid argument supplied for foreach()
eval()'d code on line 9
To debug, here's what I usually do:
- Copy and paste your entire code and place them into a standalone .php file, say
- Then in your article, use DirectPHP with only one statement:
Note that you must use the full path to your .php file.
- First try if test1.php works standalone. Any error message?
Very Important: You might need to change the setting in php.ini to display error and warning messages. If you do not see any error messages, try to check your php.ini and change the setting for error_reporting to:
error_reporting = E_ALL | E_NOTICE
- Now load the page in Joomla. Does it work? Did you see any error message?
- Once you have debugged your .php file, you can then copy and paste the code in
test1.php back into your article.
Can DiectPHP runs all PHP codes
Please note that DirectPHP is a Joomla plugin. As the name suggests, a plugin is run within a Joomla article.
What this means is that NOT ALL PHP scripts can be run within the Joomla framework.
Below are some cases in which PHP might not run properly:
- DirectPHP runs within a Joomla article. By the time DirectPHP processes your PHP codes, the HTML header for your Joomla article has already been displayed. If your PHP also outputs another HTML header (which is very common for a standard standalone .php file), it might conflict with the Joomla framework.
- Special PHP library. Some .php codes require special PHP library which might not be present in the PHP running on your host. In this case, your .php file might not work.
- Mysql database. Each Joomla installation has its own underlying mysql database. If your .php file uses its own mysql database, you must MAKE SURE that after running your codes, your last statement must connect the mysql database back to the mysql database that your Joomla is using. Otherwise you will find that your Joomla site will stop working at the end of your code because it has no way to connect back to the Joomla environment.