我們?cè)谲浖_(kāi)發(fā)的時(shí)候,經(jīng)常要進(jìn)行調(diào)試,其中有一種調(diào)試方法就是斷點(diǎn),此時(shí)可以用PHP的Xdebug擴(kuò)展來(lái)使用。
1, 安裝PHP-Xdebug擴(kuò)展;
sudo apt-get install php-xdebug
安裝完成后,用 php -v的命令查看結(jié)果
php -v PHP 5.6.30-12~ubuntu16.04.1+deb.sury.org+1 (cli) Copyright (c) 1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans
可以看到已經(jīng)有Xdebug v2.5.5安裝好了.
2, 在php的配置文件中對(duì)xdebug擴(kuò)展進(jìn)行配置,這里有兩種方式:
a: 最直接的方式,是找到php.ini,在最后面加入以下配置項(xiàng);
sudo vim /etc/php/5.6/fpm/php.ini
zend_extension=/usr/lib/php/20131226/xdebug.so xdebug.remote_host=localhost xdebug.remote_port=9100 xdebug.remote_enable=1 xdebug.remote_autostart=1
b: 另一種是在Xdebug的配置文件中,加入相同的配置項(xiàng);
我們可以在/etc/php/5.6/fpm/conf.d中看到
20-xdebug.ini -> /etc/php/5.6/mods-available/xdebug.ini
可以進(jìn)入這個(gè)文件中,看到這里已經(jīng)講zend_extension的路徑添加進(jìn)去了,將其他配置添加進(jìn)去
zend_extension=xdebug.so
以上兩種方法是等價(jià)的,因?yàn)閜hp的配置文件會(huì)引入各個(gè)擴(kuò)展的配置文件
我們逐項(xiàng)來(lái)看看這些配置分別代表什么意思
① xdebug.remote_host=localhost
Type:string, Default value:localhost
Selects the host where the debug client is running, you can either use a host name, IP address, or \'unix:///path/to/sock\' for a Unix domain socket. This setting is ignored ifxdebug.remote_connect_backis enabled.
Support for Unix domain sockets was introduced in Xdebug 2.6.
選擇debug客戶(hù)端在那個(gè)主機(jī)上運(yùn)行,可以設(shè)置成一個(gè)host名稱(chēng),IP地址,或者Unix域名的路徑. 如果xdebug.remote_connect_back被設(shè)置為 enabled時(shí),此host設(shè)置將被忽略。
② xdebug.remote_port=9100
Type:integer, Default value:9000
The port to which Xdebug tries to connect on the remote host. Port 9000 is the default for both the client and the bundled debugclient. As many clients use this port number, it is best to leave this setting unchanged.
這是Xdebug嘗試連接的遠(yuǎn)程host的端口. 9000端口是客戶(hù)端和隨附的debug客戶(hù)端的默認(rèn)端口。由于很多客戶(hù)端都使用這個(gè)端口號(hào),這個(gè)配置一旦設(shè)好,最好保持不變。
③xdebug.remote_enable=1
Type: boolean, Default value: 0
This switch controls whether Xdebug should try to contact a debug client which is listening on the host and port as set with the settings xdebug.remote_host and xdebug.remote_port. If a connection can not be established the script will just continue as if this setting was 0.
此開(kāi)關(guān)控制著Xdebug是否應(yīng)該嘗試聯(lián)系debug客戶(hù)端,這個(gè)客戶(hù)端正在監(jiān)聽(tīng)配置好的host和port. 如果這項(xiàng)配置為0,則當(dāng)無(wú)法連接時(shí),腳本會(huì)啥都不做,繼續(xù)執(zhí)行。
④ xdebug.remote_autostart=1
Type:boolean, Default value:0
Normally you need to use a specific HTTP GET/POST variable to start remote debugging (seeRemote Debugging). When this setting is set to 1, Xdebug will always attempt to start a remote debugging session and try to connect to a client, even if the GET/POST/COOKIE variable was not present.
通常你需要使用一個(gè)特定的HTTP GET/POST 變量來(lái)啟動(dòng)遠(yuǎn)程bugging. 當(dāng)此配置為1時(shí), Xdebug將永遠(yuǎn)會(huì)試圖遠(yuǎn)程debugging session,并嘗試連接一個(gè)客戶(hù)端,即便 GET/POST/COOKIE變量沒(méi)有出現(xiàn)。
3, 重啟php服務(wù)
sudo service php5.6-fpm restart
4, 在IDE中添加Debug的Host和Port,進(jìn)入PhpStorm->File->Settings->Languages & Frameworks->Servers->+
保存后,此時(shí)可以在具體的代碼文件頁(yè)面看到debug的標(biāo)識(shí),
在這里單擊一下,有一個(gè)紅色的圓圈,代表在此處設(shè)置一個(gè)斷點(diǎn)(break point)
用Postman請(qǐng)求到這個(gè)文件,可以看到在IDE下方,出現(xiàn)了很多信息
這些變量值直接展示出來(lái)了,而不要再另外去打斷點(diǎn)來(lái)查看。
當(dāng)然,以上只是xdebug很小的一部分功能。更重要的是用來(lái)做性能分析,代碼邏輯優(yōu)化等,這個(gè)過(guò)一段時(shí)間再來(lái)更新。
標(biāo)題名稱(chēng):PHP-Xdebug在PhpStorm中的使用
網(wǎng)頁(yè)網(wǎng)址:http://chinadenli.net/article20/chojjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、動(dòng)態(tài)網(wǎng)站、品牌網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、企業(yè)網(wǎng)站制作、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)