PHP Xdebug Installation and use of detailed _php tips

Source: Internet
Author: User
Tags php debugger php download
Why do you need debugger?
Many PHP programmers debug using Echo, Print_r (), Var_dump (), printf (), and so on, but for programmers with richer development experience, they can often By outputting the value of a particular variable, you can tell whether the program is performing correctly, or even how efficient it can be (some time functions may be required, of course). So why do we need a special debugging program to monitor our program's operation? The answer to this question may well be left behind to reveal.
What is Xdebug?
Xdebug is an open source PHP debugger (a debug tool) that you can use to track, debug, and analyze the state of your PHP program.
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 Setup files (Linux)
2. Installation
Win: Place the downloaded DLL file in the appropriate directory. For example, I put D:\xampp\php\ext below;
Linux: Performing 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 extension modules) Install Phpize:sudo apt-get Install Php5-dev
If 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 xdebug.so files under 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 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, the content is <?php phpinfo (), if the output of the contents of the see Xdebug, the installation configuration is successful. the following figure:

Now let's start by introducing Xdebug from the simplest program debugging.
Debugging:
Let's write a program that causes an error to occur, such as trying to include a file that does not exist.
testxdebug.php
<?php
Require_once (' abc.php ');
?>
And then through the browser, we were surprised to find that the error message became colored:

But in addition to the style change, and we usually print error message content is no different, not very meaningful. Okay, let's go ahead and rewrite the program:testxdebug2.php
<?php
Testxdebug ();
function Testxdebug () {
require_once (' abc.php ');
}
?>
Output information:

What did you find out? Xdebug the execution of the trace code, and found the function Testxdebug () that was wrong.
We write the code a little more complicated:
testxdebug3.php

Copy Code code as follows:

<?php
Testxdebug ();
function Testxdebug () {
Requirefile ();
}
function Requirefile () {
Require_once (' abc.php ');
}
?>

Output information:

That is, Xdebug has similar Java exception "Trace Backtracking" function, can follow the execution of the program step-by-step tracking to the exact location of the error, even if the call in the program is very complex, we can also through this function to clarify the relationship between the code, rapid positioning , quick to row the wrong.

In fact , PHP function debug_backtrace () also has a similar function, but note that debug_backtrace () function only in PHP4.3.0 after the version and PHP5 to take effect. This function is a new function of the PHP development team in PHP5 and then migrated back to PHP4.3 .

How to use Xdebug test script execution time
To test the execution time of a piece of script, we usually need to use the microtime () function to determine the current time. For example, in the PHP manual:

Copy Code code as follows:

<?php
/**
* 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 a microsecond number and an absolute timestamp (for example, "0.03520000 1153122275"), is not readable. So as on the program, we need to write a different functionMicrotime_float (), to add the two together.
XdebugWith a functionXdebug_time_index ()To display the time.
How do I measure the memory consumed by a script?

Sometimes we want to know how much memory it takes to get the program to a certain stage.,ThisPhpProvides a functionMemory_get_usage ()。 This function is only whenPhpCompile-time using the-enable-memory-limitparameter is valid.

Xdebugalso 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 you detect deficiencies in your code?

Sometimes the code has no obvious writing errors, no error messages (such as error, warning, notice, etc.), but this does not indicate that the code is correct. Sometimes it may take too long for a piece of code to take up so much memory that it affects the efficiency of the entire system, and we have no way to directly see what part of the code is out of the question. At this point we want to monitor the operation of each phase of the code, write to the log file, run for a period of time after the analysis, to find the problem.
Recall that before 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 the analysis file of the execution to the. /projects/xdebugdirectory (you can replace it with any directory you want to set up). If you execute a program, and then open the appropriate directory, you can find that a heap of files have been generated, such as cachegrind.out.1169585776 file named in this format. These are the analysis files generated by Xdebug. With the editor, you can see a lot of details about how the program works,

At last:

Xdebug provides a variety of functions, and the existing PHP functions are overridden, 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 of the program running, Improve the efficiency of the program, thus improving the performance of the whole system.

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.