Installation and use of PHP xdebug _php Tutorial

Source: Internet
Author: User
Tags php download
Why do I need debugger?
Many PHP programmers debug using Echo, Print_r (), Var_dump (), printf () and so on, in fact, for developers with a richer development experience these are enough, they can often in the process of program execution, By outputting the value of a particular variable, you can tell whether the program is executing correctly, or even how efficient it may be (some time functions, of course). So why do we need a dedicated debug program to monitor our program's operation? The answer to this question may be left behind to reveal.
What is Xdebug?
Xdebug is an open source PHP program debugger (a debug tool) that can be used to track, debug, and analyze the health of PHP programs.
How do I install Xdebug? :
1. Open http://www.xdebug.org/download.php Download the appropriate version
Win:windows binaries Version
Linux:source
Get a DLL file (win) or run the installation file (Linux)
2. Installation
Win: Place the downloaded DLL file in the appropriate directory. For example, I put D:\xampp\php\ext below;
Linux: Executing installation files
Tar-xvzf xdebug-2.1.2.tgz
CD xdebug-2.1.2
Phpize (if Phpize does not have this command, it needs to be installed once phpize. Phpize can have PHP support extensions) installed Phpize:sudo apt-get install Php5-dev
If you have installed continue with the following command
./configure
Make
Make install
There will be this interface



CP Modules/xdebug.so/usr/lib/php5/20090626+lfs Move the xdebug.so file below PHP5
3. To edit php.ini, 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 following directory ": /projects/xdebug "For the directory where you want to place the Xdebug output data file, you can set it freely.
4. Restart Apache;
5. Write a test.php, content , if you see Xdebug in the output content, indicating that the installation configuration was successful. such as:

now let's start with the simplest program debug step-by-step introduction Xdebug.
Debugging:
Let's start by writing a program that can cause an error to occur, such as trying to include a file that does not exist.
testxdebug.php
require_once (' abc.php ');
?>
Then through the browser access, we were surprised to find that the error message has become color:

However, in addition to the style changes, and we usually print the error message content is not very different, the meaning is not significant. OK, we continue to rewrite the program:testxdebug2.php
Testxdebug ();
function Testxdebug () {
require_once (' abc.php ');
}
?>
Output information:

What did you find out? Xdebug the execution of the tracking code, and found the function Testxdebug () that went wrong.
Let's write the code a little bit more complicated:
testxdebug3.php
Copy the Code code as follows:
Testxdebug ();
function Testxdebug () {
Requirefile ();
}
function Requirefile () {
Require_once (' abc.php ');
}
?>

Output information:

Which means Xdebug has a Java - like Exception "Trace backtracking" feature that can be traced to the exact location of the error according to the execution of the program, even if the call in the program is complex, We can also use this function to clarify the code relationship, rapid positioning, quick troubleshooting.

actually PHP functions debug_backtrace () have similar functionality, but be aware that the debug_backtrace () function is only PHP4.3.0 later versions and The PHP5 is not effective. This function is a new function of the PHP development team in PHP5 , and is then ported to PHP4.3 in reverse.

How to test script execution time with Xdebug
test the execution time of a script, usually we need to useMicrotime ()function to determine the current time. For examplePhpExamples in the manual:
Copy CodeThe 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";
?>

butMicrotime ()The value returned is the number of microseconds and the absolute timestamp (for example, "0.03520000 1153122275"), no readability. So as for the procedure above, we need to write a separate functionMicrotime_float ()to add the two together.
Xdebugcomes with a function.Xdebug_time_index ()to show the time.
How can I determine the memory that the script occupies?

Sometimes we want to know how much memory the program takes when it executes to a certain stage.,for thisPhpprovides a functionMemory_get_usage (). This function is only available whenPhpcompile-time using the-enable-memory-limitparameter is not valid.

XdebugIt also provides a functionxdebug_memory_usage ()to achieve such a function, in additionXdebugalso provides axdebug_peak_memory_usage ()function to see the peak memory consumption.

How do I detect deficiencies in my code?

Sometimes the code has no obvious coding errors, no error messages (such as error Warning notice 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 "

The purpose of these lines is to write an analysis file of the execution to the ". /projects/xdebug"directory (you can replace it with any directory you wish to set). If you execute a program and then open the appropriate directory, you can see that a bunch of files have been generated, such as a file named cachegrind.out.1169585776 in this format. These are the analysis files generated by Xdebug. With the editor open you can see a lot of details about the program running,

At last:

Xdebug provides a variety of self-contained functions, and some of the existing PHP functions to overwrite, can be easily used to debug debugging, Xdebug can also track the operation of the program, through the analysis of log files, We can quickly find the bottleneck where the program is running, improve the efficiency of the program, and improve the performance of the whole system.

http://www.bkjia.com/PHPjc/327680.html www.bkjia.com true http://www.bkjia.com/PHPjc/327680.html techarticle Why do I need debugger? Many PHP programmers debug using Echo, Print_r (), Var_dump (), printf () and so on, in fact, for the more experienced programmers have more than enough, ...

  • 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.