I find programming in PHP quite frustrating. Quite often I will try and run the script and just get a blank screen back. No error message, just empty screen. The cause might have been a simple syntax error (wrong bracket, missing semicolon), or a failed function call, or something else entirely.
It is very difficult to figure out what went wrong. I end up commenting out code, entering "echo" statements everywhere, etc. trying to narrow down the problem. But there surely must be a better way, right?.
So, is there a way to get PHP to produce useful error message like Java does? Can anyone recommend good PHP debugging tips, tools and techniques?
In addition to all the wonderful answers here, I'd like to throw in a special mention for the MySQLi and PDO libraries.
In order to...
The best option is to configure the libraries to throw exceptions.
MySQLi
Add this near the top of your script
This is best placed before you use
new mysqli()
ormysqli_connect()
.PDO
Set the
PDO::ATTR_ERRMODE
attribute toPDO::ERRMODE_EXCEPTION
on your connection instance. You can either do this in the constructoror after creation
For quick, hands-on troubleshooting I normally suggest here on SO:
to be put at the beginning of the script that is under trouble-shooting. This is not perfect, the perfect variant is that you also enable that in the
php.ini
and that you log the errors in PHP to catch syntax and startup errors.The settings outlined here display all errors, notices and warnings, including strict ones, regardless which PHP version.
Next things to consider:
See as well:
error_reporting()
Docsdisplay_errors
Docsif you are a ubuntu user then goto your terminal and run this command
where it will display recent 50 errors. There is a error file
error.log
for apache2 which logs all the errors.PHP Configuration
2 entries in php.ini dictate the output of errors:
display_errors
error_reporting
In production,
display_errors
is usually set toOff
(Which is a good thing, because error display in production sites is generally not desirable!).However, in development, it should be set to
On
, so that errors get displayed. Check!error_reporting
(as of PHP 5.3) is set by default toE_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
(meaning, everything is shown except for notices, strict standards and deprecation notices). When in doubt, set it toE_ALL
to display all the errors. Check!Whoa whoa! No check! I can't change my php.ini!
That's a shame. Usually shared hosts do not allow the alteration of their php.ini file, and so, that option is sadly unavailable. But fear not! We have other options!
Runtime configuration
In the desired script, we can alter the php.ini entries in runtime! Meaning, it'll run when the script runs! Sweet!
These two lines will do the same effect as altering the php.ini entries as above! Awesome!
I still get a blank page/500 error!
That means that the script hadn't even run! That usually happens when you have a syntax error!
With syntax errors, the script doesn't even get to runtime. It fails at compile time, meaning that it'll use the values in php.ini, which if you hadn't changed, may not allow the display of errors.
Error logs
In addition, PHP by default logs errors. In shared hosting, it may be in a dedicated folder or on the same folder as the offending script.
If you have access to php.ini, you can find it under the
error_log
entry.My usual problem are "little, stupid" parser errors which unfortunately do not show up.
However, when a .PHP-File includes a file that has parser-errors, they are shown! So I had the idea of writing a little "executor-script" that is launched with the name of the buggy file as argument, i.e.
example.com/sx.php?sc=buggy.php
It had already saved me from a lot of headache, maybe it will be helpful to someone else, too :)
sx.php
And turn on display errors in php.ini