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 a useful error message as Java does? Can anyone recommend good PHP debugging tips and techniques?
The following enables all errors:
Also see the following links
There is a really useful extension called "xdebug" that will make your reports much nicer as well.
You can enable full error reporting (including notices and strict messages). Some people find this too verbose, but it's worth a try. Set
error_reporting
toE_ALL | E_STRICT
in your php.ini.E_STRICT
will notify you about deprecated functions and give you recommendations about the best methods to do certain tasks.If you don't want notices, but you find other message types helpful, try excluding notices:
Also make sure that
display_errors
is enabled in php.ini. If your PHP version is older than 5.2.4, set it toOn
:If your version is 5.2.4 or newer, use:
You can register your own error handler in PHP. Dumping all errors to a file might help you in these obscure cases, for example. Note that your function will get called, no matter what your current error_reporting is set to. Very basic example:
To persist this and make it confortale, you can edit your php.ini file. It is usually stored in
/etc/php.ini
or/etc/php/php.ini
, but more localphp.ini
's may overwrite it, depending on your hosting provider's setup guidelines. Check aphpinfo()
file forLoaded Configuration File
at the top, to be sure which one gets loaded last.Search for display_errors in that file. There should be only 3 instances, of which 2 are commented.
Change the uncommented line to:
You might also want to try PHPStorm as your code editor. It will find many PHP and other syntax errors right as you are typing in the editor.