Build a PHP development environment on Windows

Updated at:
Copy as MD

Use Visual Studio Code (VS Code) to set up a PHP development environment on Windows.

Step 1: Install VS Code

  1. Go to the VS Code official website and click Download for Windows to download the installation file.

  2. Double-click the downloaded file, such as VSCodeUserSetup-x64-1.89.1.exe, and follow the installation wizard to install VS Code.

    Important

    In the Select Additional Tasks step, you must select Add to PATH (requires shell restart).

Step 2: Create a PHP project

  1. Open VS Code, and click Extensions in the left-side navigation pane or press Ctrl+Shift+X to 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.

  2. 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.

  3. Copy the following PHP code to the editor:

    <?php
       echo "Hello, World!";
  4. 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:8000 in the terminal to start the PHP built-in web server, and then open http://localhost:8000/index.php in your browser. The page displays Hello, World!.

      Contents of the index.php file:

      <?php
          echo "Hello, World!";
      Terminal output and server log:
      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
      Visiting http://localhost:8000/index.php in a browser displays "Hello, World!".

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