This article provides a detailed analysis of the installation and use of phpXdebug. For more information, see why Debugger is required?
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 we will introduce Xdebug step by step from the simplest program debugging.
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 ();
Function testXdebug (){
Require_once ('ABC. php ');
}
?>
Output information:
What have you found? Xdebug tracks code execution and finds the function testXdebug () with an error ().
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 say, Xdebug has the "tracing" function similar to the Exception of Java. it can track the specific error location step by step based on program execution, even if the calls in the program are very complicated, we can also use this function to clarify the code relationship, quickly locate the problem, and quickly troubleshoot the problem.
In fact, the PHP function debug_backtrace () also has similar functions, but note that the debug_backtrace () function only takes effect in versions later than PHP4.3.0 and PHP5. This function is a new function added by the PHP development team in PHP5, and is then reversely transplanted to PHP4.3.
How to use Xdebug to test the script execution time
To test the execution time of a script, we usually need to use the microtime () function to determine the current time. Example in the PHP 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 ";
?>
However, the values returned by microtime () are microseconds and absolute timestamps (for example, "0.03520000 1153122275"), which are not readable. Therefore, for the above program, we need to write another function microtime_float () to add the two.
Xdebug comes with the xdebug_time_index () function 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. For this reason, PHP provides the memory_get_usage () function (). This function is valid only when the-enable-memory-limit parameter is used during PHP compilation.
Xdebug also provides the xdebug_memory_usage () function to implement this function. In addition, xdebug also provides an 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 information (such as error, warning, or notice) is displayed, 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 that we edited the php. ini file.
Join
[Xdebug]
Xdebug. profiler_enable = on
Xdebug. trace_output_dir = "I: \ Projects \ xdebug"
Xdebug. profiler_output_dir = "I: \ Projects \ xdebug"
These lines are used to write the execution analysis file to the "../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, such as files named in the format of cachegrind. out.1169585776. These are the analysis files generated by Xdebug. Open it in the editor and you will see a lot of details about the program running,
Finally:
Xdebug provides various built-in functions and overwrites some existing PHP functions to facilitate debugging and troubleshooting. Xdebug 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.