PHP syntax check as you type with Emacs

For those who have to code in PHP, there’s a nice feature in Emacs that makes your coding horror times less stressing and helps you avoid typos and similar dumb errors.

Emacs 22.1 comes with flymake mode, a nice tool that makes syntax checking while you type out the file by highlighting the lines with errors and displays the error messages.

You can enable flymake to check PHP syntax by adding the following code on your .emacs or whatever Emacs customizations file you use:

;; Flymake PHP Extension
(require 'flymake)
(unless (fboundp 'flymake-php-init)
  (defun flymake-php-init ()
    (let* ((temp-file (flymake-init-create-temp-buffer-copy
               'flymake-create-temp-inplace))
       (local-file (file-relative-name
            temp-file
            (file-name-directory buffer-file-name))))
      (list "php" (list "-f" local-file "-l")))))
(let ((php-ext-re "\.php[345]?\'")
      (php-error-re
       "\(?:Parse\|Fatal\) error: \(.\) in \(.\) on line \([0-9]+\)"))
  (unless (assoc php-ext-re flymake-allowed-file-name-masks)
    (add-to-list 'flymake-allowed-file-name-masks
         (list php-ext-re
                       'flymake-php-init
                       'flymake-simple-cleanup
                       'flymake-get-real-file-name))
    (add-to-list 'compilation-error-regexp-alist-alist
         (list 'compilation-php
                       php-error-re  2 3 nil nil))
    (add-to-list 'compilation-error-regexp-alist 'compilation-php)
    (add-to-list 'flymake-err-line-patterns
         (list php-error-re 2 3 nil 1))))
;; add php flymake support
(add-hook 'php-mode-hook (lambda () (flymake-mode t)))

Its very nice to have on the fly syntax checking.

Creative Commons License
The PHP syntax check as you type with Emacs by Gabriel Saldaña, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Share Alike 2.5 Mexico License.

Related posts:

  1. Easy PHP code test development with SimpleTest unit testing framework
  2. How to install PHP PDO extensions on Debian Lenny
  3. Post to WordPress blogs with Emacs & Org-mode
  4. PHP 5 OOP implementation quirks
  5. Easy CSS editing with Emacs

About Gabriel Saldaña

Web developer and free software advocate.
This entry was posted in Emacs, GNU/Linux Free Software & Open Source and tagged , , . Bookmark the permalink.

One Response to PHP syntax check as you type with Emacs

  1. BadGirl13 says:

    For many staff, this can be an eye- opening experience. ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>