php 检查 xdebug 是否正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14046501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:30:17  来源:igfitidea点击:

Check if xdebug is working

phpxdebug

提问by oshirowanen

Without installing a texteditor or an IDE, is it possible to test if xdebug is working, i.e. if it can debug php code?

在不安装文本编辑器或 IDE 的情况下,是否可以测试 xdebug 是否正常工作,即它是否可以调试 php 代码?

The only part xdebug comes up in phpinfo() is the following:

xdebug 出现在 phpinfo() 中的唯一部分如下:

Additional .ini files parsed /etc/php5/apache2/conf.d/mysql.ini, /etc/php5/apache2/conf.d/mysqli.ini, /etc/php5/apache2/conf.d/pdo.ini, /etc/php5/apache2/conf.d/pdo_mysql.ini, /etc/php5/apache2/conf.d/xdebug.ini

额外的 .ini 文件解析 /etc/php5/apache2/conf.d/mysql.ini, /etc/php5/apache2/conf.d/mysqli.ini, /etc/php5/apache2/conf.d/pdo.ini, /etc/php5/apache2/conf.d/pdo_mysql.ini, /etc/php5/apache2/conf.d/xdebug.ini

It is not mentioned in the phpinfo() anywhere else.

在其他任何地方的 phpinfo() 中都没有提到它。

回答by Spudley

Without actually doing some debugging, I guess you can't be certainthat a debugger is working.

如果不实际进行一些调试,我想您无法确定调试器是否正在工作。

But you can be pretty sure -- I guess one should assume that if some aspects of xDebug are working then it would all be working.

但是你可以很确定——我想人们应该假设如果 xDebug 的某些方面正在工作,那么它就会全部工作。

Given that, you can confirm that xDebug is installed and in place by trying the following:

鉴于此,您可以通过尝试以下操作来确认 xDebug 已安装并就位:

1) phpinfo()-- this will show you all the extensions that are loaded, including xDebug. If it is there, then it's a safe bet that it's working.

1) phpinfo()-- 这将显示所有加载的扩展,包括 xDebug。如果它在那里,那么可以肯定它正在工作。

2) If that isn't good enough for you, you can try using the var_dump()function. xDebug modifies the output of var_dump()to include additional information. If this is in place, then xDebug is working.

2)如果这对您来说还不够好,您可以尝试使用该var_dump()功能。xDebug 修改 的输出var_dump()以包含附加信息。如果这到位,则 xDebug 正在工作。

3) xDebug modifies PHP's error output. If your program crashes with xDebug in place, you'll get more information about the failure than with the standard PHP crash output.

3) xDebug 修改PHP 的错误输出。如果您的程序在使用 xDebug 时崩溃,您将获得比标准 PHP 崩溃输出更多的关于失败的信息。

4) xDebug also adds a number of helper functions to PHP. You could try any of these to see if it's working. For example, the function xdebug_get_code_coverage()should exist and return an array. If it does, then xDebug is installed. If not, it isn't.

4) xDebug 还为 PHP 添加了一些辅助函数。您可以尝试其中任何一种,看看它是否有效。例如,该函数xdebug_get_code_coverage()应该存在并返回一个数组。如果是,则安装了 xDebug。如果没有,那就不是。

回答by Luke Madhanga

Run

php -m -c

in your terminal, and then look for [Zend Modules]. It should be somewhere there if it is loaded!

在您的终端中,然后查找[Zend Modules]. 如果它被加载,它应该在那里!

NB

NB

If you're using Ubuntu, it may not show up here because you need to add the xdebug settings from /etc/php5/apache2/php.iniinto /etc/php5/cli/php.ini. Mine are

如果您使用Ubuntu的,它可能不是在这里,因为你需要从添加的XDebug设置显示/etc/php5/apache2/php.ini/etc/php5/cli/php.ini。我的是

[xdebug]
zend_extension = /usr/lib/php5/20121212/xdebug.so
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=localhost
xdebug.remote_port=9000

回答by KsaR

Try as following, should return "exists" or "non exists":

尝试如下,应该返回“存在”或“不存在”:

<?php
echo (extension_loaded('xdebug') ? '' : 'non '), 'exists';

回答by Théo T. Carranza

After a bitter almost 24 hours long run trying to make xdebug to work with Netbeans 8.0.2, I have found a solution that, I hope, will work for all Ubuntu and Ubuntu related stacks.

在尝试使 xdebug 与 Netbeans 8.0.2 一起工作的近 24 小时的痛苦尝试之后,我找到了一个解决方案,我希望它适用于所有 Ubuntu 和 Ubuntu 相关堆栈。

Problem number 1: PHP and xdebug versions must be compatible

问题一:PHP 和 xdebug 版本必须兼容

Sometimes, if you're running a Linux setup and apt-get to install xdebug, it won't get you the proper xdebug version. In my case, I had the latest php version but an old xdebug version. That must be due to my current Xubuntu version. Software versions are dependent on repositories, which are dependent on the OS version you are running.

有时,如果您正在运行 Linux 安装程序并使用 apt-get 来安装 xdebug,它不会为您提供正确的 xdebug 版本。就我而言,我有最新的 php 版本,但有一个旧的 xdebug 版本。那一定是由于我当前的 Xubuntu 版本。软件版本取决于存储库,而存储库取决于您运行的操作系统版本。

Solution: PHP has a neat extension manager called PECL. Follow the instructions given hereto have it up and running. First, as pointed out by a member at the comments, you should install PHP's developer package in order to get PECL to work:

解决方案:PHP 有一个简洁的扩展管理器,称为 PECL。按照此处给出的说明启动并运行它。首先,正如评论中的一位成员所指出的,您应该安装 PHP 的开发人员包以使 PECL 正常工作:

sudo apt-get install php5-dev

Then, using PECL, you'll be able to install the latest stable version of xdebug:

然后,使用 PECL,您将能够安装 xdebug 的最新稳定版本:

sudo pecl install php5-xdebug

Once you do it, the proper version of xdebug will be installed but not ready to use. After that, you'll need to enable it. I've seen many suggestions on how to do it, but the fact of the matter is that PHP needs some modules to be enabled both to the client and the server, in this case Apache. It seems that the best practice here is to use the built in method of enabling modules, called php5enmod. Usage is described here.

完成后,将安装正确版本的 xdebug,但尚未准备好使用。之后,您需要启用它。我已经看到很多关于如何做到这一点的建议,但事实是 PHP 需要为客户端和服务器启用一些模块,在这种情况下是 Apache。似乎这里的最佳实践是使用启用模块的内置方法,称为 php5enmod。此处描述用法。

Problem number 2: Enable the module correctly

问题二:正确启用模块

First, you'll need to go inside the /etc/php5 folder. In there, you'll find 3 folders, apache2, cli, and mods_available. The mods_available folder contains text files with instructions to activate a given module. The name convention is [module].ini. Take a look inside a few of them, see how they are set up.

首先,您需要进入 /etc/php5 文件夹。在那里,您会找到 3 个文件夹,apache2、cli 和 mods_available。mods_available 文件夹包含带有激活给定模块的说明的文本文件。命名约定是 [module].ini。看看其中的一些,看看它们是如何设置的。

Now you'll have to create your ini file inside mods_available folder. Create a file named xdebug.ini, and inside the file, paste this:

现在您必须在 mods_available 文件夹中创建您的 ini 文件。创建一个名为 xdebug.ini 的文件,并在该文件中粘贴以下内容:

[xdebug]
zend_extension = /usr/lib/php5/20121212/xdebug.so
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=localhost
xdebug.remote_port=9000

Make sure that the directive [xdebug]is present, exactly like the example above. It is imperative for the module to work. In fact, just copy and paste the whole code, you'll be a happier person that way. :D

确保存在指令[xdebug],与上面的示例完全一样。模块必须工作。事实上,只需复制并粘贴整个代码,这样你就会成为一个更快乐的人。:D

Note: the zend_extension path is very important. On this example it is pointing o the current version of the PHP engine, but you should first go to /usr/lib/php5 and make sure the folder that is named with numbers is the correct one. Adjust the name to whatever you see there, and while you're at it, check inside the folder to make sure the xdebug.so is really there. It should be, if you did everything right.

注意:zend_extension 路径非常重要。在此示例中,它指向 PHP 引擎的当前版本,但您应该首先转到 /usr/lib/php5 并确保以数字命名的文件夹是正确的。将名称调整为您在此处看到的任何名称,并在使用时检查文件夹内部以确保 xdebug.so 确实存在。应该是,如果你做的一切正确。

Now, with your xdebug.ini created, it's time to enable the module. To do that, open a console and type:

现在,创建 xdebug.ini 后,是时候启用该模块了。为此,请打开控制台并键入:

php5enmod xdebug

If everything went right, PHP created two links to this file, one inside /etc/php5/apache2/conf.d and other inside /etc/php5/cli/conf.d

如果一切顺利,PHP 创建了两个指向该文件的链接,一个在 /etc/php5/apache2/conf.d 中,另一个在 /etc/php5/cli/conf.d 中

Restart your Apache server and type this on the console:

重新启动 Apache 服务器并在控制台上键入:

php -v

You should get something like this:

你应该得到这样的东西:

PHP 5.5.9-1ubuntu4.6 (cli) (built: Feb 13 2015 19:17:11) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
with Xdebug v2.3.1, Copyright (c) 2002-2015, by Derick Rethans

Which means the PHP client did read your xdebug.ini file and loaded the xdebug.so module. So far so good.

这意味着 PHP 客户端确实读取了您的 xdebug.ini 文件并加载了 xdebug.so 模块。到现在为止还挺好。

Now create a phpinfo script somewhere on your web server, and run it. This is what you should see, if everything went wright:

现在在您的 Web 服务器上的某处创建一个 phpinfo 脚本,然后运行它。如果一切顺利,这就是你应该看到的:

enter image description here

enter image description here

If you see this, Apache also loaded the module, and you are probably ready to go. Now let's see if Netbeans will debug correctly. Create a very simple script, add some variables, give them values, and set a break point on them. Now hit CTRL+F5, click on "step in" on your debugger panel, and see if you get something like this:

如果你看到这个,Apache 也加载了模块,你可能已经准备好了。现在让我们看看 Netbeans 是否能正确调试。创建一个非常简单的脚本,添加一些变量,给它们赋值,并在它们上设置一个断点。现在按 CTRL+F5,单击调试器面板上的“step in”,看看你是否得到这样的信息:

xdebug in action

xdebug in action

Remember to check Netbeans configuration for debugging, under tools/options/php. It should look something like this:

记得在 tools/options/php 下检查 Netbeans 配置以进行调试。它应该是这样的:

Debugging configurations on Netbeans

Debugging configurations on Netbeans

I hope this sheds some light on this rather obscure, confusing problem.

我希望这能对这个相当晦涩、令人困惑的问题有所了解。

Best wishes!

最好的祝愿!

回答by cmb28

you can run this small phpcode

你可以运行这个小php代码

<?php
phpinfo();
?>

Copy the whole output page, paste it in this link. Then analyze. It will show if Xdebug is installed or not. And it will give instructions to complete the installation.

复制整个输出页面,将其粘贴到此链接中。然后分析。它将显示是否安装了 Xdebug。它会给出完成安装的说明。

回答by goulashsoup

Just to extend KsaRs answer and provide a possibility to check xdebug from command line:

只是为了扩展KsaR的答案并提供从命令行检查 xdebug 的可能性:

php -r "echo (extension_loaded('xdebug') ? '' : 'non '), 'exists';"
php -r "echo (extension_loaded('xdebug') ? '' : 'non '), 'exists';"

回答by Eugen Mihailescu

If you are using Eclipsethen please note that while running on XDebug mode the magic constant __FILE__ will always be evaluated to:

如果您使用的是Eclipse,请注意在 XDebug 模式下运行时,魔术常量 __FILE__ 将始终评估为:

xdebug://debug-eval

So the following check will return trueif your session is under XDebug:

因此,如果您的会话在 XDebug 下,以下检查将返回true

$is_xdebug = false !== strpos(__FILE__,'xdebug'); // true while on XDebug

回答by Paul Preibisch

in your question you mentioned that your phpinfo was stating that apache was loading xdebug's configuration in /etc/php5/apache2/conf.d/xdebug.ini In many of the instructions online you may note that they ask you to put xdebug config in php.ini (and that is what I did) HOWEVER, if the configuration is set to /etc/php5/apache2/conf.d/xdebug.ini, then you should remove the [XDebug] configuration settings from /etc/php5/apache2/php.ini and put it in /etc/php5/apache2/conf.d/xdebug.ini INSTEAD. Once I removed from /etc/php5/apache2/php.ini and put in /etc/php5/apache2/conf.d/xdebug.ini instead, and restarted apache, it worked!!

在您的问题中,您提到您的 phpinfo 指出 apache 正在 /etc/php5/apache2/conf.d/xdebug.ini 中加载 xdebug 的配置在许多在线说明中,您可能会注意到他们要求您将 xdebug 配置放入 php .ini(这就是我所做的)但是,如果配置设置为 /etc/php5/apache2/conf.d/xdebug.ini,那么您应该从 /etc/php5/apache2 中删除 [XDebug] 配置设置/php.ini 并将其放入 /etc/php5/apache2/conf.d/xdebug.ini INSTEAD。一旦我从 /etc/php5/apache2/php.ini 中删除并放入 /etc/php5/apache2/conf.d/xdebug.ini 中,然后重新启动 apache,它就成功了!!

Therefore, in your /etc/php5/apache2/conf.d/xdebug.ini, put the following:

因此,在您的 /etc/php5/apache2/conf.d/xdebug.ini 中,输入以下内容:

[XDebug]
zend_extension="/usr/lib/php5/20121212+lfs/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_port="9000"
xdebug.profiler_enable=1
xdebug.profiler_output_dir="/home/paul/tmp"

xdebug.remote_host="localhost"
xdebug.remote_handler="dbgp";
xdebug.idekey="phpstorm_xdebug"

then remove this from the /etc/php5/apache2/php.ini if you put it there as well.

然后从 /etc/php5/apache2/php.ini 中删除它,如果你也把它放在那里。

Then do:

然后做:

sudo service apache2 restart

须藤服务 apache2 重启

Then it should work!!!

然后它应该工作!!!