- How to Debug PHP Code (A Very Simple Guide)
- TABLE OF CONTENTS
- PART 1) TURN ON ERROR REPORTING
- PHP ERROR REPORTING MECHANICS
- RECOMMENDED FOR TEST SERVER
- RECOMMENDED FOR LIVE SERVER
- PART 2) COMMON MISTAKES
- FATAL ERRORS – UNDEFINED FUNCTIONS & FILES
- SYNTAX ERRORS – MISSING OPERATORS
- WARNINGS – WRONG DATA TYPE
- NOTICES – NOT REALLY IMPORTANT…
- SILENT ERRORS – WRONG RESULTS
- PART 3) ERROR TRACING & TIPS
- DUMP THE VARIABLES
- STACK TRACE
- MANUAL BREAKPOINT
- CUSTOM ERROR LOG
- USE A GOOD EDITOR OR IDE
- DOWNLOAD & NOTES
- SUPPORT
- EXAMPLE CODE DOWNLOAD
- EXTRA BITS & LINKS
- LINKS & REFERENCES
- THE END
How to Debug PHP Code (A Very Simple Guide)
Welcome to a tutorial on how to debug PHP code. When it comes to troubleshooting, some beginners foam in the mouth and go into a trance of “very difficult, only for experts”. No. Debugging is a basic skill that does not require any special tools.
- Turning on error reporting.
- Reading the error messages.
- Tracing where the problem is and fixing it.
That’s all, don’t understand what the fuss is all about. Read on for more debugging tips!
TABLE OF CONTENTS
PART 1) TURN ON ERROR REPORTING
The first step of debugging is to stop working in the dark, at least learn how to get the error message.
PHP ERROR REPORTING MECHANICS
When it comes to error reporting in PHP, there are only 3 mechanics:
- error_reporting() – Set the error reporting level. PHP has a long list of error levels, from important E_WARNING to “notice only” E_NOTICE . Will leave a link below to the full list.
- Save the error into a log file.
- log_errors specifies if you want to save the errors into a log file.
- error_log is the target log file.
- display_errors display the error on the screen.
- Don’t confuse this with error_reporting() . Error reporting sets what kinds of errors need to be reported.
- display_errors simply sets if the error should be displayed on the screen.
- That is, we can turn off display_errors but still keep the errors in a log file. But if we turn off error_reporting() , no errors will be reported at all.
RECOMMENDED FOR TEST SERVER
When you are working on your test server, just set PHP to show all errors.
RECOMMENDED FOR LIVE SERVER
- error_reporting(E_ALL & ~E_NOTICE);
- ini_set(«display_errors», 0);
- ini_set(«log_errors», 1);
- ini_set(«error_log», «PATH/error.log»);
Yep, these should be hard-coded into php.ini already. On a live server, we hide the error messages but keep them in a log file. For security purposes, we don’t want to show the error message, and script name to the user.
PART 2) COMMON MISTAKES
Now that you have eyes on an error message, the next step is to make some sense of what it means. Here are some common examples.
FATAL ERRORS – UNDEFINED FUNCTIONS & FILES
Fatal error: Uncaught Error: Call to undefined function doesnotexist() in D:\http\2a-error-fatal.php:2 Stack trace: #0
“Call to undefined function”, what does that mean? If that is not Captain Obvious enough – The function you tried to call is not defined and does not exist. Could be a silly typo mistake, or you forgot to include the file somewhere.
SYNTAX ERRORS – MISSING OPERATORS
“Syntax error on line 3”. Just trace the code back a little, there is a missing ; on line 2. This can also mean a missing ,.()<> somewhere…
WARNINGS – WRONG DATA TYPE
Warnings are not critical errors, but they are still of significant importance. For example, putting a string into a function that seemingly needs numbers instead. While this will not break the system, it may end up with the wrong results.
NOTICES – NOT REALLY IMPORTANT…
An annoying kind of “error”. Notices are often of little importance, and they are just here to remind you of stuff like “you have not defined this variable before”, or “this function has been deprecated, not recommended”.
SILENT ERRORS – WRONG RESULTS
echo add(88, 22); // Should be 110, but result is 66
These are the deadly ones. These errors do not throw any messages, are hard to spot, and the script continues to run “as usual”. You will only realize that there is an error when the scripts do not produce the correct results.
PART 3) ERROR TRACING & TIPS
Some errors are easy to spot. Just go to the script and line that the error message indicates, eyeball a few lines of code. But how do we deal with complicated scripts? Here are a few troubleshooting tips.
DUMP THE VARIABLES
[ "name" => "Foo bar", "qty" => 99 ], "234" => [ "name" => "Doge", "qty" => 88 ] ]; // (C) WHAT'S IN THE CART? print_r($_SESSION); var_dump($_SESSION);
So, what is in an array? What is in the session that may be causing problems? print_r() and var_dump() are your best friends.
STACK TRACE
In big projects, it is common to have multiple scripts sharing many library files. So which scripts and functions were previously called? Use debug_backtrace() and debug_print_backtrace() to find out.
MANUAL BREAKPOINT
// (C) DISPLAY & STOP HERE print_r($varA); echo $varB; exit(); // (D) MORE PROCESSING $varA[] = "Doge"; $varB .= "Last";
With debugging tools, we can set “breakpoints” to pause and check what is in which variables at that point. But no worries, we don’t actually need debugging tools to do that – Just output the variables that you want to check and use die() or exit() to manually stop the script at a specific point.
CUSTOM ERROR LOG
"https://DOES-NOT-EXIST.com/dummy.php", CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false ]); // (B) CURL FETCH $result = curl_exec($curl); if ($result === false) < error_log("Failed connecting to https://DOES-NOT-EXIST.com", 0); >curl_close($curl);
Remember the error log from earlier? Code ninjas can’t be sitting at the desk, monitoring, and waiting for errors to happen 24/7. So utilize the error_log() function to capture your own custom error messages.
USE A GOOD EDITOR OR IDE
Captain Obvious to the rescue! If you have not installed a good code editor, do so now. It helps to automatically indent your code, and highlight all the silly mistakes.
P.S. This is VSCode, and there are plenty of free code editors. Links below.
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
SUPPORT
600+ free tutorials & projects on Code Boxx and still growing. I insist on not turning Code Boxx into a «paid scripts and courses» business, so every little bit of support helps.
EXAMPLE CODE DOWNLOAD
Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
EXTRA BITS & LINKS
That’s all for this guide, and here is a small section on some extras and links that may be useful to you.
LINKS & REFERENCES
THE END
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!