Containing a single-step debugger that allows you to interact directly with running PHP scripts, Xdebug is an excellent way to deal with highly complex and nuanced issues without having to resort to dangerous var_dump() and exit calls.Įxample var_dump/exit call IDE integrations
#Cam350 12 how to debug crack code
While debugging in development can be difficult to do, Xdebug is a popular tool to help identify exactly what is happening as a piece of code executes. For more complicated stack traces, this can be invaluable as it gives us a lot of post-mortem information. This results in the following stack trace: Fatal error: Uncaught Exception: a thing happened! in test.php:6Īs you can see, rather than simply returning the exception message, reading the stack trace in reverse order shows that the exception was thrown on line 6, but was triggered by a call to do_the_thing() on line 3. When do_the_thing() is executed, an exception is immediately thrown. Throw new \Exception("a thing happened!") This allows you to "trace" the "stack" of operations that are performed when an error happened in order to identify exactly what went wrong, and where.Īs an example, let's take a look at the stack trace that is returned from the following (incredibly simplistic) code: do_the_thing() To be clear, a stack trace doesn't include the files and functions that are touched before the error occurred, only the chain of methods that are called as the error happened. But, what is a stack trace? In essence, it is a rundown of every file and function that is called leading up to the error. Whenever exceptions are thrown, a stack trace is usually included. Jump back to Step 3, adjust your test parameters, and try it all over again. Not every error is easy to reproduce, and may have time-based constraints or something else making it difficult to replicate in a non-production environment. If you were unable to replicate the problem in Step 4, then it's back to the drawing board. Step 5: Adjust test parameters and try again Development and staging environments are designed to be breakable without any impact on the end-users, so always always always try to break your code in a safe environment. Once you've established the circumstances that you think might throw the exception or error you are hunting down, it's time to test them. This involves doing things like mimicking the production database, the user accounts involved, and even the operating system. Before we can do this, we first need to establish some testing guidelines. Once you've determined the log lines that relate to the problem at hand, the next step is to attempt to replicate the circumstances of the error in a development environment. Step 3: Attempt to replicate circumstances This can be done through any number of log aggregation platforms, and allows you to start establishing a timeline of events without worrying about the log files being rolled over. Once you've increased the log level, the next step is to start retaining logs. There is a good chance that the problems you are experiencing have warnings or messages associated with them that don't necessarily make it into the log files by default. This allows you to see everything that is happening before and after a problem occurs. Using the methods described in Where are PHP Errors Logged?, the first step towards diagnosing any issue is to increase the log level. Debugging PHP Errors Step 1: Increase the log level That said, there are a few common steps that can be followed towards identifying and resolving errors that crop up in production. More often than not, the error reports are vague, and identifying the underlying causes can be difficult at best. Debugging PHP errors in a production environment can be one of the single most frustrating experiences as a developer.