简体   繁体   中英

How to debug PHP using VSCode IDE in Ubuntu

I need to debug PHP code using VS Code IDE. We can debug PHP using Xdebug . So I tried a lots to setup Xdebug for debugging but I failed to use Xdebug using VS Code IDE.

PHP is 8.1
Ubuntu 22.04.1 LTS 

Please help me how can I debug.

Step 1:- Install PHP Debug Extension in VS Code IDE.

Step 2:- Copy Full output of Phpinfo() . run below Command in CLI for get output of phpinfo().

php -r "phpinfo();"

Step 3:- open Xdebug wizard Page & paste copied full output of phpinfo() in the form field. after the submitting form you will get step by step Instructions related to setup Xdebug in your System.

In my system currently I used Php 8.1. see below Snapshot of Xdebug Wizard Instructions.

Ubuntu 中 Php 8.1 的 Xdebug 向导说明Follow above Instruction which one you will getting from Xdebug Wizard

Note that, above Wizard Instructions is as per my server configuration. In your case it may differ than my above snapshot. So, you must need to follow Step 2 & 3.

Step 4:- add below Configuration in your php.ini file.

[xdebug]
xdebug.mode = debug
xdebug.start_with_request = yes
    
;Note:- Below Xdebug Path is depends on your OS & your PHP Server. 
;So you need to write path of xdebug.so (in Linux) 
;OR path of xdebug.dll (in windows) 
zend_extension=/usr/lib/php/20210902/xdebug.so 

for find Extension Directory for your server (Xampp, Lampp etc. all) then, simply Run php -r "phpinfo();" command then find out extension_dir as like below snapshot

使用 cli 的 php extension_dir 路径

OR write <?php phpinfo()?> in your php file then run that php file & found out extension_dir in your output page of phpinfo.

Step 5:- Restart your Server (Apache).

Step 6:- Install Browser Extension Initiation . then start debugger in your Vs Code IDE see below snapshot.

在 VS 代码中启动调试模式以侦听 Xdebug

Now, All Done.


for referece I share other source urls

refer Xdebug Docs refer for Windows refer this for Linux

But, before starts debugging your code, if you need to check Xdebug is working proply or not then Follow below Steps. otherwise you can ignore below steps.

In Xdebug 3+. there is xdebug_info() method exits which return info of xdebug extension.

write <?php xdebug_info(); ?> <?php xdebug_info(); ?> in your php file & then run this php file.you will get output as like below snapshot

在此处输入图像描述

& for check Xdebug works properly for CLI run php -r "xdebug_info();" in cli.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM