All Products
Search
Document Center

Alibaba Cloud SDK:Build a PHP development environment on Windows

Last Updated:Aug 02, 2024

This topic describes how to develop a PHP project by using Visual Studio Code (VS Code) on Windows.

Step 1: Install VS Code

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

    222.png

  2. Double-click the downloaded file such as VSCodeUserSetup-x64-1.89.1.exe and use 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. In the EXTENSIONS panel, search for and install the following plug-ins:

    • Code Runner: runs code for programming languages such as Node.js, Python, C++, Java, PHP, and Go. You can directly run code in the editor.

    • PHP: provides full features for the PHP language, including IntelliSense, debugging, code formatting, code lens, code fixes, code validation, refactoring, PHPUnit test, and a built-in web server.image

  2. In the top navigation bar, choose File > Open Folder to select a folder in which you want to create a PHP project. In the EXPLORER panel, right-click the blank area and select New File to create a PHP project. Name the project index.php.

  3. Copy the following sample PHP code to the code editor:

    <?php
       echo "Hello, World!";
  4. Run the PHP code. If Hello, World! is returned, the development environment is built.

    • Run the php command in the terminal to run the index.php file.

      image

    • Use the Run Code plug-in to run the index.php file by right-clicking the blank area in the code editor and selecting Run Code.

      image

    • Use the built-in web server of PHP for testing. Run the php -S localhost:8000 command in the terminal to start the built-in web server of PHP, and enter http://localhost:8000/index.php in the address bar of your browser. Hello, World!. is displayed on the web page.

      imageimage

Step 3: Install dependencies

You can run the composer require vendor/package-name [:version-constraint] command in the terminal to add or update the dependencies that are required by the PHP project.

  • vendor/package-name: the name of the vendor that provides the dependency package to be installed and the name of the dependency package. Example: alibabacloud/ecs-20140526.

  • version-constraint: optional. The version or version range of the dependency package that you want to install. The value of this parameter can be a specific version number such as 4.1.7 or a range constraint. For example, ^4.1 matches all versions greater than or equal to 4.1 and less than 5.0, and ~4.1 matches all versions greater than or equal to 4.1 and less than 5.0, excluding 4.2. If you leave this parameter empty, the dependency package of the latest version is installed by default.

For example, you can run the following command to install the Elastic Compute Service (ECS) dependency package in the PHP project:

composer require alibabacloud/ecs-20140526 ^4.1