PHP
PHP gets code intelligence from phpactor (see language servers).
Running PHP files
Run File on a PHP file runs what it is:
- a script runs as
php filein its folder; - a web app’s
index.phpis served by PHP’s built-in web server on its folder:php -S 127.0.0.1:8000 -t public. The first free port from 8000 is used, and the address appears in the Run console, ready to click; - a Laravel app’s
artisanorpublic/index.phpis served asphp artisan serveserves it: the built-in server inpublic/, with Laravel’s router. Runcomposer installfirst: the router is invendor/; - a PHPUnit test file (
…Test.phpwith aTestCaseclass) runs its tests with the project’s PHPUnit (vendor/bin/phpunit, elsephpuniton your PATH), in the folder of itsphpunit.xmlorcomposer.json.
Debug File (F5) does the same with Xdebug (see Debugging PHP). Pressed in a class file of a web app, F5 serves the app that file is part of.
Debugging
PHP is debugged with Xdebug, through PHP Debug
(vscode-php-debug), the adapter VS Code uses. Debug: Start / Continue
(debug.start, F5) or Debug File runs the file as Run File
runs it, with Xdebug turned on for that run, and stops at your breakpoints:
- a script runs with the arguments set by File: Run with Arguments…;
- a web app: F5 on its
index.php, Laravel’sartisan, or any class file of the app starts PHP’s built-in web server, as VS Code’s Launch Built-in web server does. Each request you send it stops at your breakpoints and is a thread of the session. Stop ends the server; - a PHPUnit test file runs its tests. Debug Test at Caret runs only the
test the caret is in: a method named
test…, or marked#[Test]or@test. Elsewhere in the file, all its tests run.
The program’s output reaches the Debug console. PHP’s exception breakpoints (exceptions, errors, warnings, notices, deprecations, or everything) are off by default.
PHP needs the Xdebug extension installed (on Debian and Ubuntu, the
php-xdebug package), and the adapter runs on Node.js 20.18 or later. Editora
finds PHP Debug as set on the PHP (Xdebug) card of Settings → Debugging
(its phpDebug.js, or the folder of the unpacked extension), else in its own
install, else in VS Code’s PHP Debug extension (xdebug.php-debug). The card’s
Install… downloads PHP Debug 1.40.2. Editora doesn’t install PHP or Xdebug.
PHP has no Attach to Process: Xdebug connects to a debugger that listens for
it. Debug: Listen for Xdebug (debug.listenXdebug, also in Run ▸ Launch and
Attach) starts PHP Debug listening on Xdebug’s port, 9003, without a
launch.json. PHP you start yourself then stops at your breakpoints: a request
to your own web server, or a script run with XDEBUG_SESSION set, when Xdebug
is set to connect (xdebug.mode=debug, with xdebug.start_with_request=yes
or a trigger). The session listens until you stop it. A
launch.json php configuration without a
program, VS Code’s Listen for Xdebug, does the same with its own port and
path mappings.