Heim  >  Artikel  >  php教程  >  如何调试PHP之XDebug

如何调试PHP之XDebug

WBOY
WBOYOriginal
2016-07-09 09:10:191150Durchsuche

PHP调试对我来说一直是一个问题,之前一直使用echo和var_dump这种打桩模式,效率很差,这次下定决心研究一下如何debug PHP,顺便把步骤记下来,加强记忆,也供大家参考。

首先说一下,如何使用XDebug来调试PHP程序。

我的开发环境 :

1, Eclipse PDT (Eclipse IDE for PHP Developers 1.3.0.20100617-0520),我比较喜欢Eclipse这个IDE,比较强大,插件也多

2, XAMPP  version 1.7.4 (Apache 2.2.17,MySQL 5.5.8 (Community Server),PHP 5.3.5 (VC6 X86 32bit) + PEAR ),XAMPP用起来非常方便,我也懒得自己配了,另外我下载的是ZIP版本,主要程序开发和调试

3, XDebug,这个已经在XAMPP的PHP包中自带了,如果要自己下,可以去xdebug网站,但是要注意XDebug需要对于PHP的版本,编译器,以及是32Bit还是64位

4, 系统Windows 7

 

配置PHP.ini

PHP.ini位于xampp\php\下,我们先编辑这个文件,找到其中的[XDebug]节点,并去掉一下配置的注释(我这里用的是XAMPP自带的PHP5.3.5,之前PHP.ini里面可能没这个节点):

[xdebug]
;zend_extension 是php_xdebug.dl文件的地址
zend_extension = ".\ext\php_xdebug.dll"

;remote_enable让XDebug支持debug client
xdebug.remote_enable = 1

;主机地址
xdebug_remote_host = "localhost"

;Debug端口
xdebug.remote_port = 9000

;Debug协议
xdebug.remote_handler = "dbgp"

此外,需要注意,eclipse里面似乎认不出类似"\xampp\php\ext"这样的目录写法,我在此把他们改成了".\ext"

 

配置Eclipse PDT

先添加PHP Executestables,在Windows\Preferences中,如下图

image

修改Run\Debug Configuration,PHP Debugger选XDebug,PHP Executables选我们刚才填的那个,而PHP File,则是准备Debug的文件:

image

 

接下来,我们写一个测试程序

Eclipse中添加一个PHP Project,并添加一个简单的php文件,如debug.php:

debug.php

<?php
    $a = 1;
    $b = 2;
    $c = $a + $b;
    echo $c;    
?>

并且把这个文件写到Debug Confiugration中的PHP file,可以参考刚才的配置Eclipse PDT->修改Run\Debug Configuration

然后就可以Debug了,如下图:

image

以上是最基本的PHP script调试方法,仅供参考。注意这里调试的是PHP Script,也就是以命令行模式来调试。我将在之后的Blog中解释如何以Web Site来调试PHP页面文件,敬请期待

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn