Correcting errors in PHP applications with Xdebug

Source: Internet
Author: User
Tags data structures memory usage php code php debugger zend

Introduction: The Xdebug extension of PHP can help you dissect your application to find out why when a program is in error or fails. Learn how to use Xdebug to track call stacks, analyze memory usage, and view the contents of parameters and variables.

Although you can use PHP to create command-line scripts for tasks such as system management and traditional data processing, programming languages have a major impact on the performance of WEB applications. Each PHP application resides on the server during use, and the PHP application handles incoming requests through proxies, such as Apache. For each request, a typical PHP Web application will get a Web page or XML data structure after a short run.

Assuming that after a simple run, a layered WEB application-including clients, networks, HTTP servers, application code, and the underlying database-will make it difficult to isolate errors in the PHP code. Even assuming that all layers are working correctly except for the PHP code, it can be difficult to track errors in your PHP code, especially if your application uses more classes.

PHP statement Echo and Function Var_dump (), Debug_zval_dump (), and Print_r () are common and popular debugging aids that can help solve a variety of problems. However, these statements-even the more robust tools, such as PEAR Log package--, are forensics tools that must be speculated on outside the context to generate evidence.

In a way, it is a reckless practice to debug by inference. Collect and filter the data to try to infer the problems that occur. If important information is missing, you must test the code again, repeat the steps, and then start the study again. A more efficient approach is to probe applications while the program is running. You can categorize the request parameters, filter the procedure call stack, and query for any required variables or objects. You can temporarily break the application and you can receive an alert when the variable changes value. In some cases, you can interactively ask if ... What will happen? "Problems to actually affect variables.

Special applications called debuggers support this "real time" or interactive check. The debugger may start and connect to a process to control the process and monitor its memory. Or, in the case of an interpreted language, the debugger can interpret the code directly. A typical modern graphical debugger can index and browse code to easily display complex data structures in a form that conforms to human reading habits, while displaying program state, such as call stacks, intermediate outputs, and values for all variables. For example, the debugger typically classifies and describes the properties and methods of a class.

In this and the next article, I'll introduce the tools that will make PHP easier to debug. Next time, I'll focus on interactive debugging and Zend debugger--a robust debugger specifically for PHP-and explore the many features it offers. (Zend Debugger is a commercial product and is part of the Zend PHP integrated Development Environment (IDE).) I'm also going to introduce an open source PHP debugger so you don't want to spend your money on beer instead of code. However, this article will mainly introduce how to better evidence.

Similar to crime scene investigation, just more annoying.

When the code goes wrong, fails to generate a desired result, or crashes completely, you need to answer four W questions: where, what, why, and when:

The "where" is the file and line number where the application was last run.

"What" is a code of error--for example, a suspect.

"Why" is the essence of error. It may be a logical error and/or an error caused by interacting with the operating system, or both.

The "when" is the context in which the error occurred. What happened before the termination of the program? As in all criminal acts, if you can gather enough clues, the clues will help you find the culprit.

A forensics tool Xdebug (the tool used in the previous article to analyze PHP application performance), as shown in the name, provides several features that illustrate the state of the program and is a valuable research tool that should be added to the instruction system. After installation, XDEBUG will block infinite recursion (ostensibly), fix error messages about stack traces and function tracking, and monitor memory allocations, and provide additional functionality. Xdebug also includes a set of functions that you can add to your code for Run-time error diagnosis.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.