Emacs and PHP tutorial: php-mode
Posted: - Modified: | emacs, wickedcoolemacsphp-mode is responsible for syntax highlighting, indentation, and other major PHP-specific modifications to your editing environment. There are a number of PHP modes available for Emacs. In this project, you’ll learn how to set up the php-mode available from http://sourceforge.net/projects/php-mode/ . At the time of this writing, the current version is 1.4.0 and the maintainer is Aaron Hawley.
Download the latest php-mode.el from http://php-mode.sourceforge.net/ and save it to a directory in your load-path. I like to organize my Emacs Lisp files in a directory called ~/elisp. To add PHP support to your Emacs, add the following lines to your ~/.emacs:
(add-to-list 'load-path "~/elisp")
(require 'php-mode)
This configures Emacs to automatically recognize files ending in “.php”, “.phps”, “.php3”, “.php4”, “.phtml”, and “.inc” as PHP files. To associate more extensions with PHP files, add lines like this example to your ~/.emacs:
(add-to-list 'auto-mode-alist '("\\.module$" . php-mode))
(add-to-list 'auto-mode-alist '("\\.inc$" . php-mode))
(add-to-list 'auto-mode-alist '("\\.install$" . php-mode))
(add-to-list 'auto-mode-alist '("\\.engine$" . php-mode))
This associates php-mode with the extensions used by Drupal, a PHP framework. When you open a file with the specified extension, it should be highlighted according to PHP syntax.
Here are some useful commands:
TAB | c-indent-command | Indent the current line |
M-; | comment-dwim | Add a line comment, comments or uncomments the currently-selected region, or does other smart comment-related actions |
C-c C-f | php-search-documentation | Search the online PHP manual for the current word |
C-c RET | php-browse-manual | View the online PHP manual |
C-c . | c-set-style | Change coding style |
C-M-a, C-M-e | c-beginning-of-defun, c-end-of-defun | Go to the beginning or end of the current function |
C-M-h | c-mark-function | Select the current function |
M-a, M-e | c-beginning-of-statement, c-end-of-statement | Go to the beginning or end of the current statement |
Here are some variables you may wish to customize:
indent-tabs-mode | Set this to nil if you want to insert spaces instead of tabs |
case-fold-search | Set this to t if you want case-insensitive search. |
c-basic-offset | Set your tab size or number of spaces used as a basis for indentation |
You can either customize these variables globally with M-x customize or set them for php-mode. Here’s an example that sets up a buffer with the coding style recommended for Drupal:
(defun wicked/php-mode-init ()
"Set some buffer-local variables."
(setq case-fold-search t)
(setq indent-tabs-mode nil)
(setq fill-column 78)
(setq c-basic-offset 2)
(c-set-offset 'arglist-cont 0)
(c-set-offset 'arglist-intro '+)
(c-set-offset 'case-label 2)
(c-set-offset 'arglist-close 0))
(add-hook 'php-mode-hook 'wicked/php-mode-init)
You can further customize the indentation by moving the point to where the indentation needs improvement and typing C-c C-o (c-set-offset).
To try automatic indentation, press C-j (newline-and-indent). If you like that behavior, you can make it the default in php-mode by adding the following line in ~/.emacs:
(define-key php-mode-map (kbd “RET”) ‘newline-and-indent)
You may also be interested in M-x show-paren-mode, which shows the matching parenthesis, bracket or brace for the character at point. You can enable it automatically by adding the following line to your ~/.emacs:
(setq show-paren-mode t)
It’s a good idea to separate PHP and HTML code. This is not only better coding practice, but it also makes developing in Emacs much easier. php-mode focuses on PHP-specific behavior and does not have special support for HTML. Emacs has a number of packages that allow you to work with multiple modes like php-mode and html-helper-mode in a single buffer, but they don’t always work, and indentation can be confusing. If you must work with large segments of both PHP and HTML in the same file, check out MultipleModes (http://www.emacswiki.org/cgi-bin/wiki/MultipleModes) for tips.
13 comments
Trae
2010-06-04T07:45:05ZI don't have a .emacs file.
You mentioned to Andreu to create one, but I still don't know where it needs to be created?
My directory to php-mode is here
C:\emacs-22.3\lisp\php-mode-1.5.0
So where do i put the .emacs file?
Andreu
2010-06-04T14:51:04ZHello,
You have to put .emacs file i your home directory. When emacs start, he is searching for configuration file, first step is for looking in the home directory.
Pi
2011-07-18T01:26:09ZHi,
As php-mode seems no longer maintained, I have forked it http://git.piprime.fr/?p=em...
tanmay
2012-02-29T17:50:08ZHi Sacha,
I tried with your steps, but was getting error.
So I tried by removing (require 'php-mode)
And...... it worked......
Sacha Chua
2012-02-29T18:05:23Ztanmay: Do you have php-model.el in a directory in your load-path?
tanmay
2012-03-01T20:08:13Zyup....
may be that is why its working....
Fingal
2012-03-16T07:11:04ZUsing php-mode.el, I encounter strange indenting.
It seems that within an "if" block, delimited by curly brackets, each statement will normally be indented by one stop. But if within that block there is a foreach statement, that statement will be outdented by one stop, and the block it contains will be indented relative to the outdented foreach statement, so that it lines up with the contents of the "if" block. This makes the code hard to read (for me), and I wonder if the behavior is intentional, or if this is a bug.
I know that "foreach" lines are delimited by colons, just like "case" statements in C, which are similarly outdented, by c-mode. Is this the reason? Am I the only one who finds that it obscures the logical flow?
Perhaps more to the point, is there a way I can tweak this behavior?
(I tried pasting in code (with "code" and "/code" tags), but judging by the preview, *all* the indents are removed.)
Sunil Williams
2013-11-07T20:52:43ZHey guys.
I jsut stumbled across this thread. I noticed that it's a couple of years old. Also I've noticed that emacs has never had great php support.
In case anyone hasn't noticed, there's a great project called web-mode.
http://web-mode.org
This project scratched a lot of php itches that I had. Especially when it came to working with the mixed html and php in wordpress.
taariqq
2016-11-20T10:07:51ZHi Sunil,
Thanks for a great tip. A question if I may. Does this mode produce live preview like Brackets? I mean just for html and css.
Sunil Williams
2016-11-20T10:29:41ZHi Taariqq.
That capability isn't built into web-mode.
In theory it's possible to create a lisp extension that live-updates a web-browser in emacs from source in a web-mode buffer. But given that web-browsing in emacs is both a unique experience and a very edge case, there probably wouldn't be much use for such an extension.
However...
If you want to update a browser from the contents of an emacs buffer, you could use browsersync. Emacs with gulp and browsersync are all part of my usual workflow.
taariqq
2016-11-20T16:57:13ZI found a video demonstration about impatient-mode.
Don't remember where I downloaded it from, otherwise I would've shared the link.
Oh, here you go
https://www.youtube.com/wat...
sachac
2016-11-21T06:49:51ZThanks for updating this post and sharing that nifty resource! =D
taariqq
2016-11-21T09:22:42ZSure thing :)
And what is modern web development w/o Emmet. Found at emmet.io
Here is a demo video.
https://www.youtube.com/wat...
I've been using Brackets with emacs-key-bindings. Now thinking about switching to emacs and see how it goes. I'll be doing html, css, js, and php.