PHP Debugging Methods

Source: Internet
Author: User

Why do you need debugger?

PHP programmers debugging using Echo, Print_r (), Var_dump (), printf (), and so on, in fact, for programmers with richer development experience, these are enough, they can often in the process of execution, 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?

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 to 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:

Find out what. 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 <?php
Testxdebug ();
function Testxdebug () {
Requirefile ();
}
function Requirefile () {
Require_once (' abc.php ');
}
?>

Output information:

That is to say Xdebug has similar Java exception "Trace backtracking" function, can according to the execution of the program step-by-step tracking to the specific 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, rapid row error.

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, the PHP manual example: <?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";
?>

However, the value returned by Microtime () is a microsecond number and an absolute timestamp (for example, "0.03520000 1153122275") and is not readable. So as on the program, we need to write another function microtime_float () to add the two together.

Xdebug comes with a function xdebug_time_index () to display the time.

How to determine how much memory the script consumes.

Sometimes we want to know exactly how much memory is occupied by a program when it is executed to a specific stage, and this PHP provides a function memory_get_usage (). This function is only valid when PHP compiles with the-enable-memory-limit parameter.

Xdebug also provides a function xdebug_memory_usage () to implement such a function, and Xdebug also provides a xdebug_peak_memory_usage () function to see the peak memory consumption.

How to detect deficiencies in your code.

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.