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?
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.
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:
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:
Use Kint. It is combination of debugging commands on steroids. https://kint-php.github.io/kint/ It is very similar to Nette Tracy
You can include the following lines in the file you want to debug:
This overrides the default settings in php.ini, which just make PHP report the errors to the log.