Skip to content
Happy Programming Guide
Start learning
PHP

PHP Not Working in VS Code

No IntelliSense, the run button does nothing, or the debugger never stops. PHP in VS Code needs the interpreter path, one language extension, and Xdebug configured. The checks, in order.

Hands typing on a laptop keyboard

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:

Terminal
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:

JSON
{
  "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:

JSON
{
  "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:

Terminal
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:

Terminal
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:

Output
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:

JSON
{
  "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:

JSON
"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 .php extension, 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.exclude or the extension’s own exclude setting. Composer’s autoload mapping also helps it resolve namespaces; run composer 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 instead of runningRead next

Keep reading

PHP

PHP Basics

The syntax of PHP in one page: variables, strings, arrays, conditions, loops, functions and classes, with the mistakes that catch beginners.

6 min read

PHP

PHP and Databases

Connecting PHP to MySQL or PostgreSQL with PDO: prepared statements, fetching, transactions, error handling and the queries that make pages slow.

6 min read

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *