How to debug PHP code in Visual Studio Code in the services of XAMPP?
First, I turned on XAMPP and started the following services:
By the way, the PHP Version is 7.3.3. Then I checked phpinfo()
in the browser. Here is a screenshot:
And I went xdebug.org to download this version of Xdebug:
When I copied this file to C:\xampp\php\ext
, I found there is a file named php_xdebug.dll
. Whatever, I still called the Xdebug file into the path without its name changed (php_xdebug-2.7.2-7.3-vc15-x86_64.dll
).
Next, I copied a piece of code into php.ini
from an article on the Internet:
zend_extension = "C:\xampp\php\ext\php_xdebug-2.7.2-7.3-vc15-x86_64.dll"
[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_port = 9000
But I couldn't figure out why the port was 9000 and the port didn't work at all. I entered localhost:9000
in the address bar of Chrome and the results page shows that localhost rejected my connection request.
However, I still went on. I installed the "PHP Debug" extension in VS Code. The extension's launch.json
is shown below:
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
}
Finally, I created a new PHP file and debugged it. When I opened the file in the browser, the debug program didn't work. So, could you please tell me where the problem is and show me the exact debugging steps?
update: Finally, I found what the problem is, the 9000 port number is occupied! So I typed ‘netstat -ano’ in cmd and found port 9000 and the corresponding PID value. enter image description hereThen type 'taskkill /PID 4380 /F' to kill the process that occupies the port. Finally, restart the PHP services. Thus, this issue has been solved. But remember, make sure you install the correct xdebug version, which you can view in XAMPP's phpinfo(). Thanks to the friends in the comment area for your help.
Solution 1:
You dont need to open 9000 port, you just run debug like:
then
choose PHP
start and play green icon
add break point into your methods or controllers
manage your breakpoints