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
Turn on Error Display
To debug any code, you need to know what is the error or warning message first. So the first thing is to get PHP Errors to display.
- Using PHP.ini - By default, most of the php.ini setting is to turn off error display. To turn it on, go to php.ini and make sure that the error display is turned on.
display_errors = on error_reporting = E_ALL
php_flag display_errors on php_value error_reporting 32767The value 32767 is the value for
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); . . . ?>
However, this doesn't make PHP to show parse errors - the only way to show those errors is to modify your php.ini with this line:
display_errors = on
How to Debug
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:
- 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, please see the above to see how to turn on error display.
- 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.phpback into your article.
<?php require("fullpath/test1.php"); ?>
Note that you must use the full path to your .php file.
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:
- HTML header. 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.