Skip to content

Latest commit

 

History

History
118 lines (100 loc) · 5.49 KB

Developing-PHP-in-Visual-Studio-Code-for-Dummies.md

File metadata and controls

118 lines (100 loc) · 5.49 KB

This article describes how to set up your tooling if you want a free and lightweight option for developing PHP projects on Windows (tested for PHP 8).

Install PHP

  1. Download PHP from http://windows.php.net/download/. I'm using the VC18 x64 Non Thread Safe version
  2. Extract it to c:\php
  3. Copy contents of php.ini-production to php.ini
  4. Edit php.ini and uncomment extension_dir = “ext”
  5. Uncomment some basic extensions
extension=bz2
extension=curl
extension=fileinfo
extension=gd
extension=intl
extension=mbstring
extension=exif      ; Must be after mbstring as it depends on it
extension=openssl

Install XDebug

  1. Go to c:\php

  2. SHIFT+Right click in the folder -> Open PowerShell window here

  3. Run ./php -i (alternatively, in case you receive 'php' not recognized, run ./php -r "phpinfo();" and press Enter)

    • In powershell you can use .\php -i | clip.exe to sent the output directly to clipboard.
  4. Copy the output

  5. Paste it here https://xdebug.org/wizard.php and follow the instructions which basically say:

  6. Click download

  7. Extract it to C:\php\ext

  8. Open c:\php\php.ini and append the XDebug section to the end of the file (if it's already there, adjust it accordingly):

    [XDebug] ; for XDebug v 2.x.x
    zend_extension = php_xdebug-3.1.3-8.0-vs16-nts-x86_64.dll ; Use the name of the DLL you copied to ext folder
    xdebug.default_enable = 1
    xdebug.scream = 1
    xdebug.coverage_enable = 1
    xdebug.profiler_enable = 0
    xdebug.profiler_enable_trigger = 1
    xdebug.profiler_output_dir = "C:\php\profiles"
    xdebug.remote_enable = 1
    xdebug.remote_autostart  = 1
    xdebug.remote_host=127.0.0.1
    xdebug.remote_port = 9003
    [XDebug]; for XDebug v3.x.x https://stackoverflow.com/questions/43783482/visual-studio-code-php-debugging-not-working/70351090#70351090
    xdebug.mode = coverage ; or debug
    xdebug.start_with_request = yes
    zend_extension = "C:\php\ext\php_xdebug-3.2.2-8.2-vs16-x86_64.dll" ; Use the name of the DLL you copied to ext folder
    xdebug.stopOnEntry = true
    xdebug.profiler_enable = off
    xdebug.profiler_enable_trigger = Off
    xdebug.profiler_output_name = cachegrind.out.%t.%p
    xdebug.output_dir ="c:\php\tmp"
    xdebug.show_local_vars=0
    xdebug.remote_handler = "dbgp"
    xdebug.client_host = "127.0.0.1"
    xdebug.log = "C:\php\tmp\xdebug.txt"
    xdebug.client_port = 9003
    xdebug.remote_cookie_expire_time = 36000
  9. Add c:\php to your PATH environment variable

  10. Add key: XDEBUG_CONFIG with value: idekey=VSCODE to your System Variables

See the differences for multiple XDebug configurations https://stackoverflow.com/a/70351090/3529135.

Install Visual Studio Code

  1. Download and install VS Code https://code.visualstudio.com/

  2. Learn CTRL+SHIFT+P to run any command (aka Command Palette)

  3. Use the Command Palette shortcut and search for "Inst Ext"

  4. Install Composer into your OS from https://getcomposer.org/doc/00-intro.md#installation-windows _ You might experience error during installation, validate the php_xdebug-XYZ.dll file name from error is the same as in the C:\php\ext dir. Change name or config appropriately. . Install the following extensions: _ Composer _ You can also download it separately: https://getcomposer.org/download/ _ You must set composer.executablePath user setting as: "composer.executablePath": "C:\\ProgramData\\ComposerSetup\\bin\\composer.bat" _ You will be unable to use this plugin unless you configure this setting before first use. _ Validate correct settings via Composer: Validate _ PHPUnit _ Can be installed also via Composer by running composer global require phpunit/phpunit _ PHP IntelliSense _ PHP Debug * XML Tools

  5. Use the Command Palette to "Open User Settings (JSON)"

  6. Configure PHP-related settings

    {
      "php.executablePath": "C:\\php\\php.exe",
      "php.validate.executablePath": "C:\\php\\php.exe",
      "php.validate.enable": true,
      "php.validate.run": "onType",
      "phpunit.execPath": "C:\\Users\\<yourusername>\\AppData\\Roaming\\Composer\\vendor\\bin\\phpunit.bat",
      "phpunit.args": []
    }

Done

You're ready to start coding in PHP!

Part 2: Continue reading about debugging, unit testing, autoloading, bootstrapping...

More resources

More detailed resources that helped me assemble this tutorial: