Configure Emacs for Web Development (PHP)

Daniel Martin
4 min readNov 21, 2024

--

Configure Emacs for Web Development (PHP) -Swaraa Tech

Configuring Emacs for PHP web development involves setting up several tools and packages to enhance your workflow. Emacs is highly customizable, so you can tailor it to your needs for PHP development with syntax highlighting, code completion, debugging, version control, and more.

Here’s a guide to configuring Emacs for PHP development.

________________________________________________________________

Step 1: Install Emacs

First, you need to have Emacs installed. You can download it from the official Emacs website or install it via package managers:

  • Linux:
sudo apt-get install emacs  # For Debian/Ubuntu
sudo yum install emacs # For Fedora/RHEL
  • macOS:
brew install emacs
  • Windows:
    Download the installer from Emacs for Windows.

_________________________________________________________________

Step 2: Install Package Manager (use-package)

To manage packages in Emacs, you need a package manager. The most popular one is use-package.

  1. Open your Emacs config file (~/.emacs or ~/.emacs.d/init.el).
  2. Add the following lines to install and configure use-package:
;; Bootstrap use-package if not already installed
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))

;; Enable use-package
(eval-when-compile
(require 'use-package))

This will enable you to manage your Emacs packages efficiently.

________________________________________________________________

Step 3: Install PHP-specific Packages

1. PHP Mode

PHP Mode adds syntax highlighting, indentation, and basic PHP editing features.

Add the following to your Emacs config file to install php-mode:

(use-package php-mode
:ensure t
:config
(add-to-list 'auto-mode-alist '("\\.php\\'" . php-mode)))

This ensures PHP files (.php) are automatically opened in php-mode.

2. Company Mode (for Autocompletion)

For autocompletion, we can use company-mode, a text completion framework for Emacs.

Install it using use-package:

(use-package company
:ensure t
:config
(global-company-mode)) ; Enable company globally

You can also configure it to work specifically for PHP by adding:

(use-package company-php
:ensure t
:config
(add-to-list 'company-backends 'company-ac-php-backend))

This will provide PHP-specific autocompletion.

3. Flycheck (for Syntax Checking)

Flycheck is a modern on-the-fly syntax checker for Emacs.

Install it with:

(use-package flycheck
:ensure t
:config
(add-hook 'php-mode-hook 'flycheck-mode)) ; Enable Flycheck for PHP files

This will highlight errors as you type, providing instant feedback.

4. Projectile (for Project Management)

To manage PHP projects efficiently, install Projectile, which provides project navigation, search, and management.

(use-package projectile
:ensure t
:config
(projectile-mode +1))

Projectile allows you to switch between project directories, search files, and more.

________________________________________________________________

Step 4: Set up Version Control (Git)

You’ll likely be using Git for version control. Emacs has built-in support for Git via Magit.

Install Magit with:

(use-package magit
:ensure t)

After installation, you can use the command M-x magit-status to view your Git status and interact with Git repositories from within Emacs.

_________________________________________________________________

Step 5: Configure Emacs for PHP Debugging (Xdebug)

PHP debugging can be integrated into Emacs using RealGUD with Xdebug. To set this up:

  1. Install RealGUD:
(use-package realgud
:ensure t)

2. Set up the PHP Xdebug server in your project. You can connect Emacs to the Xdebug server by configuring the debugging settings in RealGUD.

________________________________________________________________

Step 6: Set Up Emmet Mode (for HTML/CSS/JS Support)

If you’re working with HTML, CSS, or JavaScript in addition to PHP, Emmet is a great tool for expanding abbreviations.

Install emmet-mode:

(use-package emmet-mode
:ensure t
:config
(add-hook 'php-mode-hook 'emmet-mode)) ; Enable Emmet in PHP mode

This allows you to use Emmet abbreviations inside your PHP files for quick HTML generation.

________________________________________________________________

Step 7: Optional: Integrate Composer

For PHP project management with Composer, you can run Composer commands directly from Emacs.

Install lsp-composer for integration with Composer:

(use-package lsp-composer
:ensure t)

________________________________________________________________

Step 8: Customize and Optimize

You can further optimize Emacs by adding custom keybindings, setting up themes, or configuring PHP-specific indentation styles.

For example, to set up PHP indentation style:

(setq php-mode-coding-style 'psr2)  ; PSR-2 is a common PHP coding standard

________________________________________________________________

Step 9: Reload Emacs Configuration

After making these changes to your Emacs configuration file (~/.emacs or ~/.emacs.d/init.el), restart Emacs or run M-x eval-buffer to apply the new settings.

________________________________________________________________

__

With these steps, you now have a powerful and customized PHP development environment in Emacs. The tools for syntax highlighting, autocompletion, syntax checking, project management, version control, and debugging will make you productive while working on PHP web applications.

Swaraa Tech Solutions offers top-notch Web Design Services and Web Development in Ahmedabad. Our App Development Company is known for delivering innovative solutions. We also specialize in SEO Services Ahmedabad and Social Media Marketing to boost your online presence. Experience the best with our AWS Cloud Solutions. Contact us today for a free consultation!

Follow for more!
Visit our website: www.swaraa.dev
Mail us at: info@swaraa.dev
Call us on 07935336883

--

--

No responses yet