Mac computer VSCode configures PHP development environment

1.Install PHP

First, open the terminal, install Homebrew, and enter the following command:

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 

After installing Homebrew, you can install PHP using the following command:

$ brew install php 

2. Install VS Code

If you have not installed VS Code, you can visit  the VS Code official website  to download and install it.

3.Install PHP extension

In VS Code, click the Extensions icon in the left sidebar (or select Extensions through the View menu) and enter "PHP" in the search box. You will see a series of PHP-related extensions, such as "PHP Intelephense", "PHP Debug", etc. You can install these extensions according to your needs.

  • PHP Intelephense  is a very popular PHP extension that provides code highlighting, intelligent sensing, code formatting, code jumping and other functions. Click the "Install" button to install.

  • PHP Debug  is a debugger extension for PHP that uses the Xdebug extension for debugging. Click the "Install" button to install.

4. Configure PHP execution path

In VS Code, press Cmd + , open settings, and search for "PHP Executable Path". Here, you need to enter the path to the PHP executable file.

Enter which php in the terminal and it will return the installation path of PHP. Copy this path into the "PHP Executable Path" setting.

5. Configure PHP debugging

First, you need to install the Xdebug extension in your PHP. Enter the following command in the terminal:

$ pecl install xdebug 

After the installation is complete, you need to enable Xdebug in the php.ini file. Find your php.ini file, usually by typing php --ini in the terminal.

Loaded Configuration File line shows the path to the currently loaded php.ini file. Please note this path for use in subsequent steps.

Then, open this file and add the following lines:

[XDebug] 
xdebug.remote_enable = 1 
xdebug.remote_autostart = 1 

Save and close the file, then enter php -v in the terminal to confirm that Xdebug has been successfully installed.

6. Create a PHP project

  • Create a new folder as the project root directory

  • Open the root directory in VSCode

  • Create and edit PHP files (example: index.php)

  • Search for "PHP" in "Settings" to find the "PHP › Validate: Enable" setting, and check to enable PHP syntax verification.

7. Create debug configuration

Go to "Run" in the navigation bar, select "Add Configuration", create a file named .vscode/launch.json, and then configure the debugging settings in this file.

Or go to the left and select "Debug" and then select "Add Configuration" in the "Run" drop-down menu.

8. Access index.php in the browser

  • Open your PHP project in VS Code.

  • Make sure your Xdebug is properly configured and started.

  • In the debugging panel of VS Code, select the "Launch Built-in web server" configuration and click Run.

  • VS Code will print out a message in the terminal telling you the port number the server is running on. The message format is as follows: "Development Server (http://localhost:xxxx) started". The xxxx is your port number.

  • Open the browser and enter "http://localhost:xxxx/index.php", replacing xxxx with the port number you saw in step 4.

 

Guess you like

Origin blog.csdn.net/u012881779/article/details/134451951