VS Code does not ship PHP support beyond syntax colouring, so “PHP not working” means one of three things is missing: the editor cannot find a PHP interpreter, no language extension is installed to provide IntelliSense and errors, or the debugger is not connected to Xdebug. Each is a five-minute fix once you know which one you are looking at, and this walks through them in the order they usually go wrong.
Step 1: can VS Code find PHP?#
Open the integrated terminal (Ctrl+backtick) and run:
php -v
If that fails, PHP is not on the PATH VS Code inherited. Either add PHP’s folder to the system PATH and restart VS Code fully, or tell VS Code where it is:
{
"php.validate.executablePath": "C:\\xampp\\php\\php.exe"
}
On macOS with Homebrew the path is under /opt/homebrew/bin/php; on Linux, /usr/bin/php. Some extensions have their own setting for the executable, covered below.
Step 2: the language extension#
Install PHP Intelephense. Its free tier provides completion, hover documentation, go to definition, diagnostics and symbol search for most projects. PHP Tools and Devsense’s extension are commercial alternatives; the built-in PHP language features are minimal. Whichever you pick, install only one; two language servers for PHP fight over the same features and the result looks like neither is working.
After installing, reload the window and open a .php file. Intelephense indexes the workspace on first open; for a large project that takes a minute and the status bar shows progress. Until it finishes, completion is incomplete.
Disable VS Code’s built-in PHP validation to avoid duplicate error squiggles:
{
"php.suggest.basic": false,
"php.validate.enable": false
}
Step 3: running a file#
There is no built-in run button for PHP the way there is for Python. Options:
php script.php # command-line scripts
php -S localhost:8000 # serve the current folder; open http://localhost:8000/index.php
Extensions such as Code Runner add a run button that executes the current file with php; it needs the executable path configured in its own settings if PHP is not on the PATH. For web pages, the built-in server above is the simplest, and a browser is where you look at the result.
Step 4: debugging#
Breakpoints need Xdebug in PHP and the PHP Debug extension in VS Code. Check whether Xdebug is installed:
php -m | grep -i xdebug
php --ini # which php.ini to edit
If it is missing, install it with pecl install xdebug or your package manager, or use the Xdebug wizard on its website, which reads your phpinfo output and gives exact steps. Then in php.ini:
zend_extension=xdebug
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_port=9003
Restart the web server or the built-in server. In VS Code, create a launch configuration:
{
"version": "0.2.0",
"configurations": [
{ "name": "Listen for Xdebug", "type": "php", "request": "launch", "port": 9003 },
{ "name": "Run current script", "type": "php", "request": "launch", "program": "${file}", "cwd": "${fileDirname}", "port": 9003 }
]
}
Start Listen for Xdebug, set a breakpoint, load the page. If it never stops: the port in php.ini and launch.json disagree, Xdebug is loaded in the CLI but not the web server’s PHP (two different php.ini files), or a firewall is blocking the connection back to the editor.
WSL, Docker and remote#
If PHP runs inside WSL or a container, VS Code must too: open the folder with the WSL or Dev Containers extension so the terminal, Intelephense and the debugger all run where PHP is. Debugging from a container adds one setting, mapping the container’s paths to your local ones:
"pathMappings": { "/var/www/html": "${workspaceFolder}" }
And Xdebug inside the container needs xdebug.client_host=host.docker.internal to find the editor.
Other things that look like PHP not working#
- Files show as plain text. The file has no
.phpextension, or the language mode was changed; click the language in the status bar and choose PHP. - Completion works for the standard library but not your classes. Intelephense has not finished indexing, or your code is in a folder excluded by
files.excludeor the extension’s own exclude setting. Composer’s autoload mapping also helps it resolve namespaces; runcomposer dump-autoload. - Errors about undefined functions from a framework. The framework’s stubs or helper files are not in the workspace; installing dependencies with Composer makes them visible.
- Formatting does nothing. No formatter is set for PHP. Intelephense formats on its own; PHP CS Fixer and Pretty PHP are alternatives, each needing a default-formatter setting.
Questions people ask#
Do I need XAMPP to use PHP in VS Code?
No. A plain PHP install and its built-in server are enough for development. XAMPP bundles Apache and MySQL, which you need only when the project does.
Which extension: Intelephense or PHP Tools?
Intelephense’s free tier is enough for most people. Paid tiers and PHP Tools add refactoring and deeper framework support.
Why is the debugger port 9003?
Xdebug 3 changed the default from 9000 to 9003. Old tutorials say 9000; both sides must match whichever you choose.
Can I debug a Laravel or WordPress site?
Yes, the same way. Set the path mapping if the server runs elsewhere, and start listening before loading the page.
Where to go next#
- PHP code showing as plain text — when the browser, not the editor, is the problem.
- How to check which PHP version is running — the CLI versus the server.
- VS Code setup for beginners — the editor basics.