Emacs & PHP


I am doing a lot of PHP programming, and I was using emacs to do it (I switched to Zend Studio for that, and now I switched again and use Eclipse and Textmate mostly). So I wanted some more than just the syntax highlighting c-mode provides.

So I started searching. I went to freshmeat. deja.com. metacrawler. lisp archives. emacs.org. xemacs.org. And I found nothing. Well... almost nothing. Some postings and links seemed to point to something useful. This is what I found (for links to the software have a look at the bottom of the page).

Since I started this page a lot of improvements and information were sent to me. So here goes a big Thank You! to all the contributors.


Today a new release of php-mode.el was announced, that contains the improvements done by Engelke Eschner (see entry below). That was quick.
This page is not dead, it's just sleeping... Engelke Eschner notified me of an improved version of php-mode.el that now features completion (M-TAB) and parameter hints (C-.). The completion needs either a local PHP manual or a file with all function names, parameter hints rely solely on a taglist and therefor doesn't show the parameters of PHP internal functions.
Aaron Voisine emailed me an improvement on php-html mode. It solves the problem with HTML on the beginning and end of a file not being matched. See below...
Wow. That last entry was a long time ago. Anyway, I just wanted to note that the last version of php-mode written by Turadg Aleahmad now includes working speedbar support for Emacs 21 (it didn't work for me before). Great job!
Peter 'Luna' Runestig had problems with getting the tip submitted by Peter Münster (see below) to work. I included his modifcations to the hints section below.
Peter Münster tried the method suggested by Gunter Ohrner (see last news entry), but it didn't work. So he provided me with his way to get mmm to work the way he wants it... See below!
A lot of time has passed... Well, finally another minor update. This page is evolving only very slow, mostly because I am satisfied with the environment Emacs 21 and Turadg Aleahmad's php-mode provide (Aside from the speedbar not working right...). So I see this rather as a good sign... Anyway, there are people out there wo do it different. One of them is Gunter Ohrner, wo supplied me with an explanation for getting XEmacs to work with psgml-html, css-mode and php-mode using mmm-mode. More details below. Thanks!
Again Turadg Aleahmad has improved his php-mode. He moved to SourceForge and put out version 0.9.6. It seems as if this is now by far the most active project when it comes to developing an emacs php-mode... Keep up the good work!
Again Turadg Aleahmad has improved his php-mode. It has now reached 0.9.4 and features PHP support for the speedbar, manual access functions, etc.
David Tiselius informed me about the fact that Turagd Aleahmad has improved his php-mode. It has now reached 0.9.3 and I replaced my local copy (which was still 0.8) with the new one.
Finally I have the time to update again. I had a news posting by Martin Glinski on this page (some time ago). The author of this posting contacted me a while back and pointed out, that he had further improved the way to edit PHP files. The new article can be found at http://blindschleiche.de/Artikel/Emacs/20000416-emacsphp.php3.
Kevin Blake sent me a link were the PHP mode written by Turadg Aleahmad can be found. Thanks! It can be found at http://www.funq.org/turadg/software/ (broken!). This also holds a newer version, so that the statements below should probably be rewritten. I will do this sometime soon..
Finally. But the update intervals seem to be quite regular, eh? Well thanks to Caspar von Seckendorff for providing information about one additional website about mmm. See below.
Well, the big leap into the new millennium lies behind us, so we can go back to business :-) I did some optical enhancements on the site. Besides that, there is not much news...
I will try out kPHPdev, which aims to be an integrated PHP client/server based development system for PHP. Unfortunately it has nothing to do with emacs :-/ But anyway it seems to be sort of promising...
I found another php-mode.el in the news and contacted the author, Fred Yankowski. See below for details...
Well, exactly a month has gone by :-) Just coincidence, really. Well, I am surprised about the relatively high number of visitors to this page. On the other hand, so far nobody had any further suggestions :-/
A big Thanks! goes out to Gianluca Baldo for putting a link to my page on his pages at http://albasoftware.com/. You should all go and take a look there, a great place to hang out :-)
Not so much here I am afraid. This is just the second release of this page, with mmm.el and two-mode-mode as well as Jim Tom Polk´s posting added...

Search results

Hints from various people...

This is from Aaron Voisine for enhancing php-html mode:
I've got an improvement for you Emacs php-html mode. The version you have will not match html at the beginning and end of the buffer. In this example no html would be matched at all: header.inc:
       <?= $title ?>
Making html-mode the major mode does not work well for me as I get strange error messages and warning bells as I type php code. Place this in your .emacs file:
(require 'mmm-mode)
(setq mmm-global-mode 'maybe)
(setq mmm-submode-decoration-level 0)
(mmm-add-mode-ext-class nil "\\.\\(inc\\|php[s34]?\\)$" 'html-php)
(mmm-add-classes '((html-php :submode html-mode
                                 :front "\\(\\`\\|\\?>\\([^<>]*\"\\)?\\)"
                                 :back "\\(<\\?\\(=\\|php\\)?\\|\\'\\)"
                                 :include-back t)))
(autoload 'php-mode "php-mode" "PHP editing mode" t)
(add-to-list 'auto-mode-alist '("\\.\\(inc\\|php[s34]?\\)$" . php-mode))
Peter 'Luna' Runestig emailed me with this small piece of code.
I couldn't get Peter Münster's mmm-php.el to work properly; it often
misses to highlight html parts in the end of a file. So I "reversed" it,
making html-mode the main mode, and that seems to work better. Note. You
must use MMM from CVS, since the code in the "latest" tarball is very
old, and breaks c-mode indentation.

;;; mmm-php.el --- MMM submode class for PHP files
;;; Author: Peter Münster © 2002
;;; Modified by Peter 'Luna' Runestig  2002
;;; License: GPL
;; 1. Copy this file where Emacs can find it.
;; 2. Add the following lines to one of your startup files (e.g. ~/.emacs):
;;              (require 'mmm-php)
(require 'mmm-auto)
        :submode php-mode
        :front "<[%?]"
        :back "[%?]>")))
(add-hook 'html-mode-hook '(lambda ()
                (setq mmm-classes '(php-in-html))
                  'mmm-default-submode-face "LightYellow2")
(provide 'mmm-php)

Peter Münster emailed me with this small piece of code.

This makes HTML embedded in PHP work when using mmm-mode.

;;; mmm-php.el --- MMM submode class for PHP files
;;; Author: Peter Münster © 2002
;;; License: GPL
;; 1. Copy this file where Emacs can find it.
;; 2. Add the following lines to one of your startup files (e.g. ~/.emacs):
;;              (require 'mmm-php)
(require 'php-mode)
(require 'mmm-auto)
        :submode html-mode
        :front "\\?>"
        :back "<\\?")))
(add-hook 'php-mode-hook '(lambda ()
                (setq mmm-classes '(html-in-php))
                 'mmm-default-submode-face "LightBlue1")
(provide 'mmm-php)
Gunter Ohrner emailed me...

This is in german, unfortunately... but you may extract the useful things by looking at his code snippets, I suppose. Or go and get out that old german school book :-) Anyway, here it is:


Ich habe in letzter Zeit versucht, xEmacs 2.4.6 mit Hilfe des mmm-mode 0.4.7 zu der Zusammenarbeit mit psgml-html und Turadg Aleahmads php-mode zu bewegen. Ich hatte das Problem, dass in den eingebetteten PHP-Bereichen die Code-Einrückung überhaupt nicht funktioniert hat - beim Drücken von TAB ist alles in der ersten Zeile gelandet.

Nach langem Suchen des Rätsels Lösung: Mit dem aktuellen mmm-mode aus dem SourceForge CVS (http://sf.net/projects/mmm-mode/) klappt es deutlich besser.

Und mit folgender Konstruktion am Anfang der PHP-Bereiche lässt sich auch die Einrückung korrigieren, wenn der zugrundeliegende cc-mode durch den HTML-code "verwirrt" wurde bzw. wenn er aus dem syntaktischen Kontext eine Einrückung vornimmt, die nicht zu dem umgebenden HTML passt:

  <!-- { -->
  <?php ;
  echo "The PHP-Code goes here...<br>";

Ich hab' dann mit Hilfe des mmm-mode noch einen css-mode eingebunden und habe jetzt Syntaxhighlighting und Einrückungshilfen in HTML, PHP und CSS. :-) (JavaScript geht sicher auch noch, das habe ich noch nicht getestet.)

Ich hab' keine Ahnung, ob das so der optimale Weg ist, um mit xEmacs PHP in HTML zum Laufen zu kriegen, aber ich habe keinen besseren gefunden und vielleicht hilft die Info ja dem ein oder anderen...

Gunter Ohrner

PS: Die Teile meiner ~/.xemacs/init.el für die mmm-mode-Configuration:

                :submode php-mode
                :face mmm-code-submode-face
                :front "<\\?php"
                :back "\\?>"
                :submode css-mode
                :face mmm-declaration-submode-face
                :front "style=\\\""
                :back "\\\""
(add-to-list 'auto-mode-alist '("\\.inc\\'" . html-mode))
(add-to-list 'auto-mode-alist '("\\.phtml\\'" . html-mode))
(add-to-list 'auto-mode-alist '("\\.php[34]?\\'" . html-mode))
(add-to-list 'auto-mode-alist '("\\.[sj]?html?\\'" . html-mode))
(add-to-list 'auto-mode-alist '("\\.jsp\\'" . html-mode))

(add-to-list 'mmm-mode-ext-classes-alist '(html-mode nil html-js))
(add-to-list 'mmm-mode-ext-classes-alist '(html-mode nil embedded-css))
(add-to-list 'mmm-mode-ext-classes-alist '(html-mode nil fancy-html))

Dave Thomas in comp.emacs

> I'm just wondering whether an Elisp package for PHP scripting language
> is available. Any help with locating such package for Emacs will be
> greatly appreciated.

php-mode comes with the php source distribution.


Well, I didn´t find anything. I had a look into the source archives of versions 2.0.7b, 3.0.12 and 4.0b2. Unfortunately Dave Thomas didn´t go into further detail.

Jim Tom Polk in comp.emacs

The combination of HTML and PHP makes the editing somewhat of a problem, in my opinion.

What I've done instead is using html-helper-mode, modified the tables.el, renamed it php.el, byte-compiled it and put it into my .emacs file and made it a drop down menu with various common stuctures that I use. Since I mainly work with the postgreSQL db, I have a very limited set of items, like surrounding HTML and the like with echo, making while loops, pg_Exec, pg_Conn and SQL (INSERT, DELETE, UPDATE, SELECT) and of course bound to certain keystrokes.

When I'm working with a file that is primarily code (like an .inc file) I simply have my f7 and f6 keys set globally to swap between html-helper-mode and c++-mode.

So, where's the meat?

Actual elisp code...

Turadg Aleahmad´s php-mode.el

This is my favourite now! The project moved to SourceForge and improved a lot in the past weeks.

It sets up a menu, makes the speedbar recognize PHP, has good syntax highlighting, is configurable via the emacs customization interface and much more.

New Homepage is http://sourceforge.net/projects/php-mode/!

Fred Yankowski´s php-mode-el
Here is what he says in the news:

A major mode for editing PHP code is available via a link at http://www.ontosys.com/reports/PHP.html.

I wasn't able to find an existing implementation, so I created one myself using define-derived-mode to base it on c-mode. It works well, except for some glitches handling shell-style ('#') comments.

Of course you can also find the code at the bottom of this page :-).

In our further communication he stated:

I have no particular motivation to maintain a php-mode.el, so if some other PHP-mode actually subsumes mine in functionality I might well switch over to it. On the other hand, I'm willing to add functionality to my php-mode.el if there's a call for it.

What does this tell us? See the "Future-Section" below.

David N. Welton´s two-mode-mode.el

This is definitely cool... I don´t know if it is nice code, but it seems to work quite good. It is originally for sgml/tcl mode switching, but this can be changed with a quick edit. When activated the modes are switched automagically according to the cursors position. Give it a try - not only for PHP/HTML mixing. You might ant to have a look at http://blindschleiche.de/Artikel/Emacs/20000416-emacsphp.php3.

Gongquan Chen´s mmm.el

A script similar to the two-mode-mode above, but obviously for even more than two modes... I havent´t tried out this one, just had a quick look into the code so far. But it seems to be rather easy to configure and use.

At last, an example of using mmm.el by Andre E. Bar'yudin. It can be found at http://www.cs.huji.ac.il/~baryudin/php3_mode.html, and uses mmm to combine html-mode and c++-mode.

Kazunori Nishi´s php-mode.el

This is what I found first. But it was in japanese, just the LISP code and some english words standing out of a mess of characters this file confronted me with. Having no friends with a knowledge of japanese, I decided to give up on it.

Well, eventually I stumbled across a translated version. In english. Good. But as old as the original one: October 1997 it says in the source. But some good ideas are to be found there:

  • tag completion
  • tag explanation
  • php interpreter support

You type the beginning of a function, and request completion. Do that again, and a buffer explaining the function pops up. But although this is a Good Thing (tm) it has some major drawbacks the way it is realized here: The functions are contained in a seperate file, which of course would have to be kept up to date to the progress of the php language. Most of the mysql functions I use aren´t in the file from 1997 :-)

Better would be a support for direct access to the php html documentation, which could be accessed either using w3 or some external browser.

Aside from that, this mode is basically an extension to c++-mode. This is where the highlighting, indentation etc. come from.

hhm PHP support

This is originally just for editing html, as the name HTML helper mode suggest. But it has been improved to do font-locking according to PHP and ASP and can narrow the buffer to PHP/ASP/JavaScript parts, guessing the right mode to use.

A lot of functionality is present here, as this is not just an PHP mode. But since it is not a PHP mode, some improvements can be on the wishlist :-)


I started to code the PHP logic seperate from the page layout so the need for a mixed PHP/HTML mode more or less vanished. I am still using html-helper-mode for the HTML and now use Turadg Aleahmad's php-mode. I am in fact quite happy with this...

Nonetheless there are lots of improvements that could surely be done over time. Debugger support (there are PHP4 debuggers around...) would be one :)

And for the future...

Well, I will be trying to keep this site up to date, as (hopefully) new information reaches me (<- hint :-)...

Maybe someone should coordinate the work on the subject of emacs and PHP. I would be willing to do it, but I am not sure if I am the right person to do it. Why? Well, my knowledge in emacs-lisp approaches zero, unfortunately :-/ So maybe one of the gurus out there could take over this responsibility!?