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
-
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.zipfor 64-bit).
-
Extract PHP:
- Extract the downloaded zip file to a directory of your choice, e.g.,
C:\php.
- Extract the downloaded zip file to a directory of your choice, e.g.,
-
Configure PHP:
- Open the extracted PHP folder.
- Copy
php.ini-developmentand rename it tophp.ini. - Open
php.iniwith a text editor and configure the following settings:- Uncomment and set
extension_dirto 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
- Uncomment and set
-
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
Pathvariable, then click “Edit”. - Click “New” and add the path to the PHP directory, e.g.,
C:\php. - Click “OK” to close all windows.
-
Verify PHP Installation:
- Open Command Prompt and type:
php -v - You should see the PHP version information.
- Open Command Prompt and type:
macOS
-
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)"
- Open Terminal and enter the following command:
-
Install PHP:
- In Terminal, enter:
brew install php
- In Terminal, enter:
-
Verify PHP Installation:
- Enter the following command in Terminal:
php -v - You should see the PHP version information.
- Enter the following command in Terminal:
Linux (Ubuntu/Debian)
-
Update Package List:
- Open Terminal and enter:
sudo apt update
- Open Terminal and enter:
-
Install PHP:
- Enter the following command:
sudo apt install php libapache2-mod-php
- Enter the following command:
-
Verify PHP Installation:
- Enter the following command in Terminal:
php -v - You should see the PHP version information.
- Enter the following command in Terminal:
Installing Visual Studio Code
-
Download VS Code:
- Go to the official Visual Studio Code website.
- Download the installer for your operating system (Windows, macOS, or Linux).
-
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
-
Open VS Code.
-
Go to the Extensions view:
- Click the Extensions icon in the Activity Bar on the side of the window or press
Ctrl+Shift+X.
- Click the Extensions icon in the Activity Bar on the side of the window or press
-
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.
- Search for and install the following extensions:
Configure PHP in VS Code
-
Create a PHP File:
- Open a folder where you want to save your PHP files.
- Create a new file with a
.phpextension, for example,index.php.
-
Write PHP Code:
- Write some PHP code in your
index.phpfile. For example:<?php echo "Hello, World!"; ?> - Save the file by pressing
Ctrl+S.
- Write some PHP code in your
Running PHP Code
You can run your PHP script using the terminal or an integrated PHP server.
Running PHP in the Terminal
-
Open the Terminal:
- Select
Terminal>New Terminalfrom the top menu or pressCtrl+`.
- Select
-
Navigate to the Directory:
- In the terminal, navigate to the directory where your PHP file is located using the
cdcommand.
- In the terminal, navigate to the directory where your PHP file is located using the
-
Run the PHP Script:
- Enter the following command:
php index.php - You should see the output
Hello, World!in the terminal.
- Enter the following command:
Running PHP with an Integrated PHP Server
-
Open the Terminal.
-
Navigate to the Directory:
- In the terminal, navigate to the directory where your PHP file is located.
-
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 seeHello, World!displayed on the page.
- Enter the following command:
Debugging PHP Code in VS Code
-
Open VS Code and go to the Debug view:
- Click the Debug icon in the Activity Bar or press
Ctrl+Shift+D.
- Click the Debug icon in the Activity Bar or press
-
Create a Debug Configuration:
- Click on the gear icon to open the
launch.jsonconfiguration file. - Add the following configuration for PHP debugging:
{ "version": "0.2.0", "configurations": [ { "name": "Listen for XDebug", "type": "php", "request": "launch", "port": 9003 } ] }
- Click on the gear icon to open the
-
Install and Configure Xdebug:
- Follow the official Xdebug installation guide to install and configure Xdebug for your PHP installation.
-
Set Breakpoints:
- Set breakpoints in your PHP code by clicking in the gutter next to the line numbers.
-
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.phpfile 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
Altto add a cursor. - Use
Ctrl+Alt+Down/Upto add cursors above or below the current line.
- Click while holding
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+\.
- Open the terminal with
3. Code Snippets
- Description: Use predefined or custom code snippets to quickly insert commonly used code blocks.
- Usage:
- Type a snippet prefix and press
Tabto insert it. - Create custom snippets by opening
Preferences: Configure User Snippetsfrom the Command Palette (Ctrl+Shift+P).
- Type a snippet prefix and press
4. Command Palette
- Description: The Command Palette provides quick access to various commands and settings.
- Usage: Open it with
Ctrl+Shift+Pand 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*5and pressingTab).
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.