Windows和Linux中php代码调试工具Xdebug的安装与配置详解
一、为什么需要Debugger?
很多PHP程序员调试使用echo、print_r()、var_dump()、printf()等,其实对 于有较丰富开发经验的程序员来说这些也已经足够了,他们往往可以在程序执行的过程中,通过输出特定变量的值可以判断程序执行是否正确,甚至效率高低也可以 看出来(当然可能还需要使用一些时间函数)。那么我们为什么还需要一个专门的调试程序来监控我们的程序运行呢? 这个问题的答案不妨留到后面来揭晓。
二、什么是Xdebug?
Xdebug是一个开放源代码的PHP程序调试器(即一个Debug工具),可以用来跟踪,调试和分析PHP程序的运行状况。
三、Windows 下 XDebug 安装与配置
1. 下载XDebug二进制文件: http://www.xdebug.org/download.php
请根据php版本选择下载,例如:
复制代码 代码如下:
5.2 http://www.xdebug.org/files/php_xdebug-2.1.2-5.2-vc6.dll
5.3 http://www.xdebug.org/files/php_xdebug-2.1.2-5.3-vc6.dll
2. 找到并打开 php.ini 文件
3. 如果配置过 ZendOptimizer, 需要先屏蔽 ZendOptimizer 有关的配置, 通常如下:
复制代码 代码如下:
[Zend]
zend_extension_manager.optimizer_ts=”path\ZendOptimizer-3.3.0\lib\Optimizer-3.3.0″
zend_extension_ts=”path\ZendOptimizer-3.3.0\lib\ZendExtensionManager.dll”
将其删除或用分号注释掉,如改为:
复制代码 代码如下:
;[Zend]
;zend_extension_manager.optimizer_ts=”path\ZendOptimizer-3.3.0\lib\Optimizer-3.3.0″
;zend_extension_ts=”path\ZendOptimizer-3.3.0\lib\ZendExtensionManager.dll”
4. 加入 XDebug 配置。参考如下:
复制代码 代码如下:
[Xdebug]
zend_extension_ts=”path/xdebug/php_xdebug-2.1.2-5.2-vc6.dll”
xdebug.auto_trace=on
xdebug.trace_output_dir=”path\xdebug”
xdebug.profiler_enable=on
xdebug.profiler_output_dir=”path\xdebug”
xdebug.collect_params=on
xdebug.collect_return=on
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000
说明:
上面 “path” 的地方需要修改为你自己的本地路径.
参数解释:
复制代码 代码如下:
zend_extension_ts=”c:/webserver/php5/ext/php_xdebug.dll”
;加载xdebug模块。这里不能用extension=php_xdebug.dll的方式加载,必须要以zend的方式加载,否则安装上后,phpinfo是显示不出xdebug这个项的。
xdebug.auto_trace=on;
;自动打开“监测函数调用过程”的功模。该功能可以在你指定的目录中将函数调用的监测信息以文件的形式输出。此配置项的默认值为off。
xdebug.collect_params=on;
;打开收集“函数参数”的功能。将函数调用的参数值列入函数过程调用的监测信息中。此配置项的默认值为off。
xdebug.collect_return=on
;打开收集“函数返回值”的功能。将函数的返回值列入函数过程调用的监测信息中。此配置项的默认值为off。
xdebug.trace_output_dir=
;设定函数调用监测信息的输出文件的路径。
xdebug.profiler_enable=on
;打开效能监测器。
xdebug.profiler_output_dir=
;设定效能监测信息输出文件的路径。
还有一些更为具体的参数设定,详见:http://www.xdebug.org/docs-settings.php
5. 重启web服务器,如 Apache 或者 IIS
6. 查看 phpinfo 的输出, 如果看到 XDebug 的选项, 就说明配置成功了.
7. 调试信息文件查看。
在本地运行php程序,会在所设定的目录里产生一些调试信息的文件,主要包括:
a. 函数调用过程监测信息文件,文件名格式:trace.××××××.xt。该文件可直接查看,里面包含了函数运行的时间,函数调用的参数值,返回值,所在的文件和位置等信息。内容格式相对直观。
b. 效能监测文件,文件名格式:cachegrind.out.××××××××。
该文件也可以直接查看,但信息格式不易被人类所理解,我们可以安装 wincachegrind 软件,以格式化地读取它。 下载安装方法参考如下:
下载:http://sourceforge.net/projects/wincachegrind/
下载后安装运行,然后点击Tools->options,设定 working folder(php.ini里 xdebug.profiler_output_dir 的值)
这样就可以比较直观的查看效能监测文件的信息了。
四、linux下 XDebug 安装与配置
linux 下可以下载源代码编译安装,方法参考如下。
1.下载对应 php 版本的源代码 source: http://www.xdebug.org/download.php
例如xdebug-2.1.2.tgz版本:http://www.xdebug.org/files/xdebug-2.1.2.tgz
2. 编译安装
复制代码 代码如下:
tar -xvzf xdebug-2.1.2.tgz
cd xdebug-2.1.2
./configure
make
make install
如果有报错 phpize 没有这个command,那么安装它:
复制代码 代码如下:
sudo apt-get install php5-dev
3. 将xdebug.so文件移到php5下面
复制代码 代码如下:
cp modules/xdebug.so /usr/lib/php5/
4. 编辑php.ini,加入下面几行:
复制代码 代码如下:
[Xdebug]
zend_extension= /usr/lib/php5/xdebug.so
xdebug.profiler_enable=on
xdebug.trace_output_dir=”../xdebug”
xdebug.profiler_output_dir=”../xdebug”
5. 重启Apache,测试是否安装成功
如果输出的内容中有看到xdebug,说明安装配置成功。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

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 is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

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.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.
