fervor [>]CODING & CURIOSITY
FERVOR LEARNING SYSTEMTUTORIALS
← PHP

PHP / 8 MIN READ

Installing PHP on Your Computer

Let's go through the detailed steps for installing PHP on your computer and setting up Visual Studio Code (VS Code) for PHP development. Installing PHP on

From the original Fervor library. Examples may use older package versions.

Let’s go through the detailed steps for installing PHP on your computer and setting up Visual Studio Code (VS Code) for PHP development.

Installing PHP on Your Computer

Windows

  1. Download PHP:

    • Go to the official PHP website.
    • Under the “Windows downloads” section, download the latest PHP zip file (e.g., php-8.x.x-Win32-vs16-x64.zip for 64-bit).
  2. Extract PHP:

    • Extract the downloaded zip file to a directory of your choice, e.g., C:\php.
  3. Configure PHP:

    • Open the extracted PHP folder.
    • Copy php.ini-development and rename it to php.ini.
    • Open php.ini with a text editor and configure the following settings:
      • Uncomment and set extension_dir to the ext folder path:
        extension_dir = "ext"
        
      • Enable necessary extensions by uncommenting them (remove ; at the beginning of the line), e.g.,:
        extension=curl
        extension=gd
        extension=mbstring
        extension=openssl
        extension=pdo_mysql
        
  4. Add PHP to the System PATH:

    • Open the Start menu and search for “Environment Variables”.
    • Click on “Edit the system environment variables”.
    • In the System Properties window, click the “Environment Variables” button.
    • In the “System variables” section, find and select the Path variable, then click “Edit”.
    • Click “New” and add the path to the PHP directory, e.g., C:\php.
    • Click “OK” to close all windows.
  5. Verify PHP Installation:

    • Open Command Prompt and type:
      php -v
      
    • You should see the PHP version information.

macOS

  1. Install Homebrew (if not already installed):

    • Open Terminal and enter the following command:
      /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
      
  2. Install PHP:

    • In Terminal, enter:
      brew install php
      
  3. Verify PHP Installation:

    • Enter the following command in Terminal:
      php -v
      
    • You should see the PHP version information.

Linux (Ubuntu/Debian)

  1. Update Package List:

    • Open Terminal and enter:
      sudo apt update
      
  2. Install PHP:

    • Enter the following command:
      sudo apt install php libapache2-mod-php
      
  3. Verify PHP Installation:

    • Enter the following command in Terminal:
      php -v
      
    • You should see the PHP version information.

Installing Visual Studio Code

  1. Download VS Code:

  2. Install VS Code:

    • Run the downloaded installer and follow the prompts to complete the installation.

Setting Up PHP in Visual Studio Code

Install PHP Extensions for VS Code

  1. Open VS Code.

  2. Go to the Extensions view:

    • Click the Extensions icon in the Activity Bar on the side of the window or press Ctrl+Shift+X.
  3. Search and Install Extensions:

    • Search for and install the following extensions:
      • PHP Intelephense: Provides rich PHP language support.
      • PHP Debug: Adds support for PHP debugging.
      • PHP DocBlocker: Helps with PHP documentation.

Configure PHP in VS Code

  1. Create a PHP File:

    • Open a folder where you want to save your PHP files.
    • Create a new file with a .php extension, for example, index.php.
  2. Write PHP Code:

    • Write some PHP code in your index.php file. For example:
      <?php
      echo "Hello, World!";
      ?>
      
    • Save the file by pressing Ctrl+S.

Running PHP Code

You can run your PHP script using the terminal or an integrated PHP server.

Running PHP in the Terminal

  1. Open the Terminal:

    • Select Terminal > New Terminal from the top menu or press Ctrl+`.
  2. Navigate to the Directory:

    • In the terminal, navigate to the directory where your PHP file is located using the cd command.
  3. Run the PHP Script:

    • Enter the following command:
      php index.php
      
    • You should see the output Hello, World! in the terminal.

Running PHP with an Integrated PHP Server

  1. Open the Terminal.

  2. Navigate to the Directory:

    • In the terminal, navigate to the directory where your PHP file is located.
  3. Start the Built-in PHP Server:

    • Enter the following command:
      php -S localhost:8000
      
    • Open your web browser and navigate to http://localhost:8000/index.php. You should see Hello, World! displayed on the page.

Debugging PHP Code in VS Code

  1. Open VS Code and go to the Debug view:

    • Click the Debug icon in the Activity Bar or press Ctrl+Shift+D.
  2. Create a Debug Configuration:

    • Click on the gear icon to open the launch.json configuration file.
    • Add the following configuration for PHP debugging:
      {
        "version": "0.2.0",
        "configurations": [
          {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9003
          }
        ]
      }
      
  3. Install and Configure Xdebug:

  4. Set Breakpoints:

    • Set breakpoints in your PHP code by clicking in the gutter next to the line numbers.
  5. Start Debugging:

    • Click the green play button in the Debug view.

By following these steps, you will have a fully functional PHP development environment set up in Visual Studio Code. This setup will allow you to write, run, and debug PHP code efficiently.

Bonus Cool Vs Code

Certainly! Visual Studio Code (VS Code) is a highly versatile and customizable code editor that can be enhanced with various extensions and features. Here are some cool things you can add and do in VS Code to improve your PHP development experience:

Cool Extensions for PHP Development

1. PHP Intelephense

  • Description: Provides intelligent code completion, real-time error reporting, code navigation, and more for PHP.
  • Installation: Search for “PHP Intelephense” in the Extensions view (Ctrl+Shift+X).

2. PHP Debug

  • Description: Adds debugging support for PHP, enabling you to set breakpoints, inspect variables, and step through code.
  • Installation: Search for “PHP Debug” in the Extensions view.

3. PHP DocBlocker

  • Description: Helps with generating and managing PHPDoc comments for better documentation of your code.
  • Installation: Search for “PHP DocBlocker” in the Extensions view.

4. PHP Sniffer & Beautifier

  • Description: Integrates PHP_CodeSniffer and PHP-CS-Fixer to enforce coding standards and automatically format your code.
  • Installation: Search for “PHP Sniffer & Beautifier” in the Extensions view.

5. Blade Snippets

  • Description: Adds syntax highlighting and snippets for Laravel Blade templates.
  • Installation: Search for “Blade Snippets” in the Extensions view.

6. Path Intellisense

  • Description: Provides autocomplete suggestions for file paths, making it easier to include files and assets.
  • Installation: Search for “Path Intellisense” in the Extensions view.

Customizing VS Code

1. Themes and Icons

  • Description: Customize the look and feel of VS Code with different themes and icon packs.
  • Popular Themes:
    • One Dark Pro: A popular dark theme inspired by Atom’s One Dark theme.
    • Dracula Official: A dark theme with a modern color scheme.
    • Solarized Dark and Light: Themes based on the Solarized color palette.
  • Popular Icon Packs:
    • Material Icon Theme: Adds Material Design icons to your file explorer.
    • VSCode Icons: A popular icon pack with a wide variety of icons.
  • Installation: Search for themes and icon packs in the Extensions view.

2. Live Server

  • Description: Launch a local development server with live reload feature for static and dynamic pages.
  • Installation: Search for “Live Server” in the Extensions view.
  • Usage: Right-click on your index.php file in the Explorer and select “Open with Live Server”. Note that for PHP files, you may need to configure the server to use PHP’s built-in server.

3. Bracket Pair Colorizer

  • Description: Colorizes matching brackets to make it easier to identify code blocks.
  • Installation: Search for “Bracket Pair Colorizer” in the Extensions view.

4. Auto Rename Tag

  • Description: Automatically renames paired HTML/XML tags when you change the opening or closing tag.
  • Installation: Search for “Auto Rename Tag” in the Extensions view.

5. Prettier - Code Formatter

  • Description: Automatically formats your code according to specified style rules.
  • Installation: Search for “Prettier - Code Formatter” in the Extensions view.
  • Usage: Configure Prettier as the default formatter in your VS Code settings.

6. Settings Sync

  • Description: Syncs your VS Code settings, extensions, and keybindings across multiple devices.
  • Installation: Search for “Settings Sync” in the Extensions view.
  • Usage: Set up an account with GitHub to sync your settings.

Productivity Tips

1. Multi-Cursor Editing

  • Description: VS Code supports multiple cursors, allowing you to edit code simultaneously in different places.
  • Usage:
    • Click while holding Alt to add a cursor.
    • Use Ctrl+Alt+Down/Up to add cursors above or below the current line.

2. Integrated Terminal

  • Description: VS Code includes an integrated terminal that you can use to run commands without leaving the editor.
  • Usage:
    • Open the terminal with Ctrl+`.
    • Split the terminal with Ctrl+\.

3. Code Snippets

  • Description: Use predefined or custom code snippets to quickly insert commonly used code blocks.
  • Usage:
    • Type a snippet prefix and press Tab to insert it.
    • Create custom snippets by opening Preferences: Configure User Snippets from the Command Palette (Ctrl+Shift+P).

4. Command Palette

  • Description: The Command Palette provides quick access to various commands and settings.
  • Usage: Open it with Ctrl+Shift+P and start typing the command you need.

5. IntelliSense and Emmet

  • Description: VS Code’s IntelliSense provides intelligent code completion based on variable types, function definitions, and imported modules. Emmet provides shorthand syntax for generating HTML and CSS.
  • Usage:
    • Start typing and VS Code will show suggestions.
    • Use Emmet abbreviations in HTML and CSS files (e.g., typing div.container>ul>li*5 and pressing Tab).

Conclusion

By installing these extensions and using these features, you can significantly enhance your PHP development workflow in Visual Studio Code. The flexibility and extensibility of VS Code make it an excellent choice for web development, providing powerful tools and customization options to fit your needs.

Keep your curiosity going.Explore more PHP →
287 TUTORIALS · 22 TOPICSREADY