Build a PHP development environment on Windows
Use Visual Studio Code (VS Code) to set up a PHP development environment on Windows.
Step 1: Install VS Code
-
Go to the VS Code official website and click Download for Windows to download the installation file.
-
Double-click the downloaded file, such as VSCodeUserSetup-x64-1.89.1.exe, and follow the installation wizard to install VS Code.
ImportantIn the Select Additional Tasks step, you must select Add to PATH (requires shell restart).
Step 2: Create a PHP project
-
Open VS Code, and click Extensions in the left-side navigation pane or press
Ctrl+Shift+Xto open the Extensions panel. Search for and install the following extensions:-
Code Runner: runs code directly in the editor for languages such as Node.js, Python, C++, Java, PHP, and Go.
-
PHP: Provides full-featured PHP support, including IntelliSense, debugging, formatting, code lens, code fixes, validation, refactoring, PHPUnit testing, a built-in web server, and more. To install it, search for PHP in the Extensions: Marketplace, locate the PHP (DEVSENSE) extension, and click Install.
-
-
In the top navigation bar, choose File > Open Folder to open your project folder. In the Explorer panel, right-click the blank area, select New File, and name the project index.php.
-
Copy the following PHP code to the editor:
<?php echo "Hello, World!"; -
Run the PHP code. If
Hello, World!is returned, the environment is ready.-
Run the php command in the terminal to execute index.php.
<?php echo "Hello, World!";PS D:\ide\phpprojects> php index.php Hello, World! PS D:\ide\phpprojects> -
Right-click the blank area in the editor and select Run Code to run
index.php.<?php echo "Hello, World!";[Running] php "d:\ide\phpprojects\index.php" Hello, World! [Done] exited with code=0 in 0.131 seconds -
Run
php -S localhost:8000in the terminal to start the PHP built-in web server, and then openhttp://localhost:8000/index.phpin your browser. The page displays Hello, World!.Contents of the
index.phpfile:
Terminal output and server log:<?php echo "Hello, World!";
Visiting http://localhost:8000/index.php in a browser displays "Hello, World!".PS D:\ide\phpprojects> php index.php Hello, World! PS D:\ide\phpprojects> php -S localhost:8000 [Wed Jul 3 15:31:05 2024] PHP 7.4.33 Development Server (http://localhost:8000) started [Wed Jul 3 15:31:15 2024] [::1]:55732 Accepted [Wed Jul 3 15:31:15 2024] [::1]:55732 [200]: GET /index.php [Wed Jul 3 15:31:15 2024] [::1]:55732 Closing [Wed Jul 3 15:31:15 2024] [::1]:55736 Accepted [Wed Jul 3 15:31:15 2024] [::1]:55737 Accepted [Wed Jul 3 15:31:15 2024] [::1]:55736 [404]: GET /favicon.ico - No such file or directory [Wed Jul 3 15:31:15 2024] [::1]:55736 Closing
-
Step 3: Install dependencies
Run the composer require vendor/package-name [:version-constraint] command to add or update dependencies for your PHP project.
-
vendor/package-name: the vendor and package name. Example: alibabacloud/ecs-20140526.
-
version-constraint: optional. A specific version such as 4.1.7 or a range constraint. For example, ^4.1 matches versions greater than or equal to 4.1 and less than 5.0, and ~4.1 matches versions greater than or equal to 4.1 and less than 5.0, excluding 4.2. If omitted, the latest version is installed.
For example, install the Elastic Compute Service (ECS) dependency package:
composer require alibabacloud/ecs-20140526 ^4.1