Home >Backend Development >PHP Tutorial >Detailed explanation of the installation and use of php Xdebug_PHP tutorial

Detailed explanation of the installation and use of php Xdebug_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:05:421003browse

Why do you need Debugger?
Many PHP programmers use echo, print_r(), var_dump(), printf(), etc. for debugging. In fact, these are enough for programmers with rich development experience. They can often During the execution of the program, you can judge whether the program is executed correctly by outputting the values ​​of specific variables, and even the efficiency can be seen (of course you may also need to use some time functions). So why do we need a special debugger to monitor the operation of our program? The answer to this question might as well be left to be revealed later.
What is Xdebug?
Xdebug is an open source PHP program debugger (i.e. 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
to get a dll file (win) or run the installation file ( linux)
2. Install
Win: Place the downloaded dll file into the corresponding directory. For example, mine is placed under D: (If phpize does not have this command, you need to install phpize once. phpize can enable PHP to support extension modules) Install phpize: sudo apt-get install php5-dev
If installed, continue with the following command
./configure
make
make install


will have this interface


cp modules/xdebug.so /usr/lib/php5/20090626+lfs Move the xdebug.so file under php5
3. Edit php.ini and add the following lines:

[Xdebug]
zend_extension=D:xamppphpextphp_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"
after The directory "../Projects/xdebug" is the directory where you want to place the data files output by Xdebug and can be set freely.

4. Restart Apache;


5. Write a test.php with the content of . If xdebug is seen in the output content, the installation and configuration are successful.

As shown below:
Now let’s introduce

Xdebug

step by step starting from the simplest program debugging.

Debugging:
We first write a program that can cause execution errors, such as trying to include a file that does not exist.
testXdebug.php

require_once('abc.php');?>
Then accessed through the browser, we were surprised to find that the error message became colorful:


However, apart from the style change, it is no different from the error message content we usually print, so it is of little significance. Okay, let’s continue to rewrite the program:

testXdebug2.php

testXdebug();function testXdebug() {
require_once
('abc.php');
}?>
Output information:


What did you find? Xdebug

traces the execution of the code and finds the faulty function testXdebug()

.

Let’s make the code more complicated: testXdebug3.php

Copy code
The code is as follows:

testXdebug();function testXdebug() { requireFile();
}
function requireFile () {
require_once('abc.php');
}
?>


Output information:

In other words,

When we get to the specific location of the error, even if the calls in the program are very complex, we can use this function to clarify the code relationship, quickly locate it, and quickly troubleshoot. In fact, the PHP function debug_backtrace()

also has similar functions, but please note that debug_backtrace() The function only takes effect in versions after PHP4.3.0 and PHP5. This function is a new function added by the PHP development team in PHP5, and then back-ported to PHP4.3. How to use Xdebug to test script execution time

To test the execution time of a certain script, we usually need to use

microtime()Function to determine the current time. For example, the example in the PHP
manual:
Copy the code 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 secondsn";
?>


But the value returned by microtime() is the number of microseconds and absolute timestamp (such as "0.03520000 1153122275"), which is not readable. So for the above program, we need to write another function microtime_float() to add the two.
Xdebug comes with a function xdebug_time_index() to display the time.
How to measure the memory occupied by a script?

Sometimes we want to know how much memory the program occupies when it reaches a certain stage of execution,for this PHP provides Function memory_get_usage(). This function is only valid when PHP is compiled with the -enable-memory-limit parameter.

A xdebug_peak_memory_usage() function to view the peak memory usage. How to detect deficiencies in code?

Sometimes the code does not have obvious writing errors and does not display any error messages (such as error, warning,

notice

, etc.), but this It does not imply that the code is correct. Sometimes a certain piece of code may take too long to execute and occupy too much memory, affecting the efficiency of the entire system. We have no way to directly see which part of the code has a problem. At this time, we hope to monitor the operation of each stage of the code, write it to the log file, and then analyze it after running for a period of time to find the problem. Recall that before we edited the php.ini file
and added
[Xdebug]
xdebug.profiler_enable=on
xdebug.trace_output_dir="I:Projectsxdebug"xdebug.profiler_output_dir="I:Projectsxdebug"
The purpose of these lines is to write the execution analysis file into the "
../Projects/xdebug
" directory (you can replace it with whatever you want to set Table of contents). If you execute a certain program and then open the corresponding directory, you can find that a bunch of files have been generated, such as files named in this format:
cachegrind.out.1169585776
. These are the analysis files generated by

Xdebug

. Open it with an editor and you can see a lot of detailed information about the program running, Finally:

Xdebug

provides various built-in functions and overwrites some existing PHP functions, which can be conveniently used for debugging and troubleshooting; XdebugYou can also track the running of the program. By analyzing the log files, we can quickly find the bottleneck of the running of the program, improve the efficiency of the program, and thereby improve the performance of the entire system.



http://www.bkjia.com/PHPjc/327680.html

www.bkjia.comtrue

http: //www.bkjia.com/PHPjc/327680.htmlTechArticleWhy do you need Debugger? Many PHP programmers use echo, print_r(), var_dump(), printf(), etc. for debugging , in fact, these are enough for programmers with rich development experience,...

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn