How To Use PHP In Visual Studio Code
Visual Studio Code is a great editor for PHP and other language development. You get almost all the features like syntax highlighting, bracket matching, PHP IntelliSense, and snippets out of the box, and you can create or add more functionality through community-created Visual Studio Code extensions.
No developer enjoys debugging web applications. But we all know that we need to use quality software debugging because it is a crucial development aspect. While the .Net and Java developers often have their language-specific IDEs that contain full debugging support, PHP developers may sometimes feel left out.
Sure, there are quite some commercial applications like phpStorm that can help with debugging PHP code. But these business applications are usually very expensive and operating system dependent. Luckily there are a few open-source code editors out there that will give PHP developers the same debugging experience without expensive IDEs, and VS Code is one of them.
If you are new to VSCode, check out how to install visual studio code on mac. That guide will help you to set up the VSCode in your Mac.
How To Use PHP In Visual Studio Code
By default, Visual Studio Code comes with PHP Support, but you can also install the PHP language extensions available on VS Code Marketplace. You can search for the PHP extensions from within the VS Code in the Extensions view (⇧⌘X), then you can select the extensions using a drop-down list by typing php. But first, check your PHP version using the following command.
I have PHP version 7.2.11, and if you have something like 5.6.* or 7.0.* or 7.1.* then it is good to update your PHP version. I have installed PHP using homebrew, so I can update the PHP version using the following command. This PHP version upgrade guide is for MAC and not for Windows.
brew upgrade php
If you are using MAMP or XAMPP, then you need to update that software bundle as well to get the latest PHP version.
PHP extensions for VSCode
PHPIntelliSense and PHP Debug are the most IMP extensions for PHP Development Environment. But there are more extensions that you can use based on your project flow or your style to write the code. Extensions will help you to fast your workflow and reduce your code repetition and also provide code completion.
To install and use the PHP Intellisense, you need at least PHP 7 installed on your machine. Then, you can either add it to your PATH or set the php.executablePath setting. If you are facing duplication, disabling VS Code’s built-in PHP IntelliSense by setting php.suggest.basic to false. Finally, you can add the php executable path inside user settings.
Please find your PHP Executable and do not copy the above code if you have a different PHP version and the way you have installed PHP previously because PHP’s path will be changed on your machine. For example, if you are using MAMP or XAMPP, then the php path is different. However, I have used Homebrew so that I can write the above path.
Features Of Intellisense
- Code Completion
- Workspace symbol search
- Signature Help
- Find all references
- Go to definition
Install the extension: Press F1 and type ext install php-debug.
This extension is the debug adapter between the VS Code and XDebug by Derick Rethan. XDebug is the PHP extension (a .so file on Linux and a .dll on Windows) that needs to be installed on your server.
Features Of PHP Debug
- Line breakpoints.
- Conditional Breakpoints.
- Function breakpoints.
- Step over, step in, step out.
- Break on entry.
- Breaking on uncaught exceptions and errors/ warnings/notices.
- Multiple, parallel requests.
- Stack traces, scope variables, superglobals, user-defined constants.
- Arrays & Objects (including class name, private and static properties).
- Debug console.
- Run as CLI.
- Run without debugging.
PHP Code Linting
VS Code uses the official PHP linter (php -l) for PHP language. It allows VS Code to stay current with PHP linter improvements.
There are three settings to control the PHP linter:
php.validate.enable: controls whether to enable PHP linting at all. Enabled by default.
php.validate.executablePath: points to the PHP executable on disk. Set this if the PHP executable is not on the system path.
php.validate.run: controls whether the validation is triggered on save (value:
"onSave") or on the type (value:
"onType"). The default is on save.
Finally, How To Use PHP In Visual Studio Code article is over.