Installation and Use of phpXdebug _ PHP Tutorial

Source: Internet
Author: User
Install and use phpXdebug. Why Debugger? Many PHP programmers use echo, print_r (), var_dump (), and printf () for debugging. In fact, this is enough for programmers with rich development experience. why do they need Debugger?
Many PHP programmers use echo, print_r (), var_dump (), and printf () for debugging. In fact, this is enough for programmers with rich development experience, during program execution, they can output the values of specific variables to determine whether the program is correctly executed, even the efficiency can be seen (of course, some time functions may need to be used ). So why do we need a special debugging program to monitor our program running? The answer to this question may be later.
What is Xdebug?
Xdebug is an open-source PHP program debugger (a Debug tool) that can be used to track, Debug, and analyze the running status of PHP programs.
How to install Xdebug? :
1. open http://www.xdebug.org/download.php to download the corresponding version.
Win: Windows binaries version
Linux: source
Obtain a dll file (win) or run the installation file (linux)
2. Installation
Win: put the downloaded dll file in the corresponding directory. For example, put my files under D: \ xampp \ php \ ext;
Linux: run the installation file
Tar-xvzf xdebug-2.1.2.tgz
Cd xdebug-2.1.2
Phpize (if phpize does not have this command, you need to install phpize once. Phpize allows php to support extension modules) install phpize: sudo apt-get install php5-dev
If the installation is complete, run the following command:
./Configure
Make
Make install
This interface is available.



Cp modules/xdebug. so/usr/lib/php5/20090626 + lfs move the xdebug. so file to php5
3. edit php. ini and add the following lines:
[Xdebug]
Zend_extension = D: \ xampp \ php \ ext \ php_xdebug.dll (Win)
Zend_extension =/usr/lib/php5/20090626 + lfs/xdebug. so (Linux)

Xdebug. profiler_enable = on
Xdebug. trace_output_dir = "../Projects/xdebug"
Xdebug. profiler_output_dir = "../Projects/xdebug"
The directory "../Projects/xdebug" after you want to place the Directory of the data file output by Xdebug, which can be set freely.
4. restart Apache;
5. write a test. php file with the content If xdebug is displayed in the output, the installation and configuration are successful.For example:

Now let's start with the simplest program debugging step by step.Xdebug.
Debugging:
We first write a program that can cause execution errors, such as trying to contain a non-existent file.
TestXdebug. php
Require_once('ABC. php ');
?>
Then, we were surprised to find that the error information was colored:

However, apart from style changes, the error information we usually print is no different and does not make much sense. Okay. let's rewrite the program:TestXdebug2.php
TestXdebug ();
FunctionTestXdebug (){
Require_once('ABC. php ');
}
?>
Output information:

What have you found?XdebugTrace code execution and find the function with an errorTestXdebug ().
The code is more complex:
TestXdebug3.php

The code is as follows:


TestXdebug ();
Function testXdebug (){
RequireFile ();
}
Function requireFile (){
Require_once ('ABC. php ');
}
?>


Output information:

That is to sayXdebugSimilarJavaOfExceptionTracking and backtracking can trace the specific error location step by step based on program execution. even if the call in the program is complicated, we can also use this function to clarify the code relationship, quickly locate and troubleshoot problems.

ActuallyPHPFunctionDebug_backtrace ()Similar functions are available, but note thatDebug_backtrace ()Function onlyPHP4.3.0Later versions andPHP5. This function isPHPThe development teamPHP5And then port it backPHP4.3.

How to use Xdebug to test the script execution time
We usually need to test the execution time of a script.Microtime ()Function to determine the current time. For examplePHPExample in the manual:

The code is as follows:


/**
* Simple function to replicate PHP 5 behaviour
*/
Function microtime_float ()
{
List ($ usec, $ sec) = explode ("", microtime ());
Return (float) $ usec + (float) $ sec );
}
$ Time_start = microtime_float ();
// Sleep for a while
Usleep (100 );
$ Time_end = microtime_float ();
$ Time = $ time_end-$ time_start;
Echo "Did nothing in $ time seconds \ n ";
?>


HoweverMicrotime () The returned value is the number of microseconds and the absolute timestamp (for example,"0.03520000 1153122275 "), No readability. So for the above program, we need to write another functionMicrotime_float () To add the two.
Xdebug Built-in function Xdebug_time_index () To display the time.
How can I determine the memory occupied by the script?

Sometimes we want to know how much memory is occupied when the program is executed to a specific stage., ThereforePHP Provides functionsMemory_get_usage () . This function only works whenPHP Used during compilation-Enable-memory-limit This parameter is valid.

Xdebug Also provides a function Xdebug_memory_usage () To implement this function.Xdebug Also provides Xdebug_peak_memory_usage () Function to view the peak memory usage.

How do I detect deficiencies in the code?

Sometimes the code has no obvious writing errors and no error message is displayed (suchError,Warning,Notice), But this does not indicate that the code is correct. Sometimes it may take too long to execute a piece of code, occupying too much memory, which affects the efficiency of the entire system. we cannot directly see which part of the code has a problem. At this time, we hope to monitor the running status of each stage of the code, write it to the log file, and analyze it after running for a period of time to locate the problem.
Recall, we edited it before.Php. iniFile
Join
[Xdebug]
Xdebug. profiler_enable = on
Xdebug. trace_output_dir = "I: \ Projects \ xdebug"
Xdebug. profiler_output_dir = "I: \ Projects \ xdebug"

The purpose of these lines is to write the execution analysis file"../Projects/xdebug"Directory (you can replace it with any directory you want to set ). If you open the corresponding directory after executing a program, you can find that a bunch of files are generated, suchCachegrind. out.1169585776Files named in this format. These areXdebugThe generated analysis file. Open it in the editor and you will see a lot of details about the program running,

Finally:

XdebugProvides a variety of built-in functions, andPHPThe function is overwritten to facilitate debugging and troubleshooting;XdebugWe can also track program running. by analyzing log files, we can quickly find the bottleneck of program running, improve program efficiency, and thus improve the performance of the entire system.

Why? Many PHP programmers use echo, print_r (), var_dump (), and printf () for debugging. In fact, this is enough for programmers with rich development experience ,...

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.