search
HomeBackend DevelopmentPHP TutorialDetailed explanation of the installation and use of php Xdebug_PHP tutorial

Detailed explanation of the installation and use of php Xdebug_PHP tutorial

Jul 21, 2016 pm 03:05 PM
debuggerphpxdebugandWhyuseInstallofprogrammerDetailed explanationdebugneed

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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool