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?
Programming in PHP can be frustrating for sure. I have spent countless hours banging my head against the wall, only to discover that I made a stupid mistake. However, I have found 3 big tips that make the error handling process much easier over the years.
Use a Syntax Checker: For me, I like to use a text editor that has a syntax checker built in. For PHP, I love the Visual Studio Code editor as it notifies you whenever you have a syntax error. This saves a ton of time and eliminates the possibility of a syntax error whilst debugging.
Use a Solid PHP Framework: Error handling can be completely different depending on the PHP framework that you use. This is one of the many reasons why I couldn't recommend the Laravel framework enough. Laravel is a Model View Controller framework and provides you with easy to set up logging to files or databases. Their debug mode is awesome as well as it helps you identify your issues much more quickly than you would in another framework.
var_dump: When dealing with different third party APIs and being unsure as to how to access different data structures, I have found var_dump to be incredibly important. Per PHP's docs, it dumps information about a variable. This means that it gives you critical information about how a variable is structured so that you can access the data held within it. Is the variable an object? Is it an array? Use var_dump to see.