Debugging a single PHP file using Vscode

Source: Internet
Author: User
Tags php server phpinfo

My blog Original: https://wiki.feiffy.cc/Blog: Debug a single php file with Vscode

Suddenly found to be able to use Vscode Single file, today has not been successful, but also thought that Vscode can not debug a single file it. Here is a record of today's "sudden discovery" process.

Start, is watching "modern PHP" this book, see the "Built-in HTTP Server" section, you test the launch of PHP built-in Server Software command: php -S localhost:4000 , successfully see the browser page shows the relevant page. The effect is the same as the Web site that Apache sets.

Then I "Suddenly" thought, could you debug the PHP code that runs on the built-in server? At this point, I am not aware of the relevant things. Just searching the web for "xdebug PHP build-in Server" and other key words, found someone else already mentioned in the StackOverflow, it is normal, usually you can ask questions, others may have already mentioned. Here's the question a person gives a link: vim-xdebug-and-php-54s-development-server, click in to see, part of the original quote is as follows:

I recently began using Xdebug and vim for debugging and breakpoints. I had been using Eclipse and Xdebug, and missed this feature when I switched back to vim.

I found a few articles on the subject, but more were fairly old, missed some key points and didn ' t mention the 5.4 develop ment server. I used this article as my starting point and I had to fill in a few gaps.

  1. First off, make sure xdebug are installed by checking phpinfo for the sections on Xdebug. I Use the Ppa/ondrej repository for managing PHP installations and is able to install Xdebug through apt. installation WA s as simple as Apt-get install Php5-xdebug.
  2. Once You has Xdebug installed, you'll need to install the Vim remote debugger interface. It is a straightforward install-just copy of the files into your plugin directory. For me, this is ~/.vim/plugin.
  3. Now, you had to modify your php.ini file in order to allow remote debugging. I also turned on remote autostart so I don't have to deal with the? Xdebug_session_start query string on my development server. Since i is using the PHP 5.4 development Server, > I had to doing some extra work to find out where my php.ini file is L ocated.
    Open up phpinfo again and check for the "Loaded Configuration File" entry. When you do this, make sure your access phpinfo as served by the 5.4 development server. If you access a phpinfo served through Apache2, it may is not being the same file. For me, the correct file > was the Default/etc/php5/cli/php.ini.

Add these lines to your php.ini to turn on remote debugging and remote Autostart.

Xdebug.remote_enable=on
Xdebug.remote_autostart=on

    1. Now the You ' ve got all of the that set up, you can give vim and xdebug a try. Open a file in vim and press F5 to initiate the connection. Then go to your browser and navigate to a page that's being served by the PHP 5.4 Development server and go back to vim. If all works well, you should see that a connection have been established. Press ENTER to begin debugging.

Follow his steps, the first step to install the Php-xdebug module, the second step I use is the Vscode xdebug plug-in, the plug-in and xdebug to communicate, the third step to modify PHP.ini, I opened the built-in server phpinfo () output page, Locate the loaded php.ini file path:/etc/php/7.0/cli/php.ini, open php.ini adds two lines of configuration:

xdebug.remote_enable=1xdebug.remote_autostart=1

After saving, restart the built-in PHP server. Xdebug Even if the configuration is good.

Then add a debug configuration to the Vscode, just as you would debug the Apache Web site before opening the XDebug port for monitoring:

test.php page Set breakpoints, open the browser, access: http://localhost:4000/test.php, you can automatically run to the breakpoint.

At this point, the code in the built-in PHP server has been successfully debugged.

But I naturally think that there is no Apache Web site, can also be successfully debugged, the reason is obviously installed php-xdebug extension, modified the php.ini configuration, and so on! , modified the configuration of PHP.ini! This php.ini configuration is the CLI under the PHP configuration, then, that is, the direct command line execution PHP script file should also be able to debug.

The configuration for debugging individual files is added in Vscode: Add the following code to the. Vscode/launch.json:

  "configurations":  [ {  "name"  :     "type"      "request"         "CWD"    " port " :  9000  }  ]

This configuration is Vscode XDebug plug-in of the default generated debug PHP single file configuration, its plug-in document has a description, long seen, but can not be debugged, a little debugging buttons run out. In other words, there is something missing in the middle that allows the XDebug plugin to capture the XDebug debug process. Now, with the addition of the CLI version of the php.ini configuration, a bit of debugging is done successfully to the breakpoint.

So finally realized, open a PHP file, directly F5 open debugging, the script will be able to run to the breakpoint. Implemented a one-click Debug php Single File script.

Debugging a single PHP file using Vscode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.