Saturday, May 24, 2008

Using Emacs to edit PHP files

When we are programming on any language a good editor(emacs :)), lint (which gives syntax errors) and interactive debugger helps us to code fast.

I want to use emacs to edit php files. There are other IDEs which supports lint and debugger but I feel much comfort with emacs. So my idea is to find good php-mode for emacs and I want to find sytax errors in emacs itself. And I want to find good interactive debugger like python debugger.


I found php-mode and compiler for php but I could not find interactive debugger in the command line for php.

PHP-mode for emacs:

http://sourceforge.net/projects/php-mode/

Download php-mode from the above location. Extract the compressed file and copy php-mode.el to emacs lisp path.
Refer this link for emacs lisp path http://www.gnu.org/software/emacs/elisp/html_node/Library-Search.html.
or you can put this php-mode.el file in some directory and you can add this directory to lisp path. To add this new directory to lisp path add this line in the .emacs file.

(add-to-list 'load-path "~/path/to/new/directory/")

And add following lines to .emacs file to enable php-mode

(require 'php-mode)

;; To use abbrev-mode, add lines like this:
(add-hook 'php-mode-hook
'(lambda () (define-abbrev php-mode-abbrev-table "ex" "extends")))

Enable Tab indentation to 2:

The php-mode indentation for arrays is not good. It will indent like this

$a = array(
'foo' => 'bar',
'gaz' => 'gazonk',
) ;

If the associated array is having another associative array. then it will be like this

$a = array(
'foo' => array(
'bar' => 'bar info')
,
'gaz' => 'gazonk',
) ;

If we are having more values like that then the code does not look good. It looks good if it will be like this.

$a = array(
'foo' => array(
'bar' => 'bar info')
,
'gaz' => 'gazonk',
) ;

To get view looks like that add following code in .emacs file

(defun clean-php-mode ()
(interactive)
(php-mode)
(setq c-basic-offset 2) ; 2 tabs indenting
(setq indent-tabs-mode nil)
(setq fill-column 78)
(c-set-offset 'case-label '+)
(c-set-offset 'arglist-close 'c-lineup-arglist-operators))
(c-set-offset 'arglist-intro '+) ; for FAPI arrays and DBTNG
(c-set-offset 'arglist-cont-nonempty 'c-lineup-math) ; for DBTNG fields and values

Enable php mode for drupal module files:

To enable php-mode for drupal module file add these lines to .emacs file

(add-to-list 'auto-mode-alist '("/drupal.*\\.\\(php\\|module\\|inc\\|test\\|install\\)$" . php-mode))
(add-to-list 'auto-mode-alist '("/drupal.*\\.info" . conf-windows-mode))

In place of /drupal you can put your drupal code base path. I put my drupal project at /var/www/ directory I replaced /drupal.* with /var/www/

PHP lint:

Hardly no body use php in the command line, so to use we need to install php-cli (php command line interface). Using php at the command line will make us to find errors easily and fast. We can find syntax error with the following command.
php -l <php file name>

To find more about php-cli option follow this link
http://www.php-cli.com/php-cli-options.shtml

I am using emacs so I can directly check syntax errors in emacs without running the above command on the command prompt. To do that add the following lines in the .emacs file.

;; run php lint when press f8 key
;; php lint
(defun phplint-thisfile ()
(interactive)
(compile (format "php -l %s" (buffer-file-name))))

(add-hook 'php-mode-hook
'(lambda ()
(local-set-key [f8] 'phplint-thisfile)))
;; end of php lint

To check syntax errors press "F8" function key on emacs editor, then currently opened php file will complie and show the syntax errors if any.

PHP Debugger: I tried to find good interactive debugger like python debugger. But I could not find it. So I might be developing one in the future.

4 comments:

Anonymous said...

I just started getting into PHP code and have been using emacs since that's what I use for everything else.
I found the info here VERY useful!

Golchi said...

Hey,

It's fine if you use Emacs. But i think you should try other IDEs. it's true that ressources (CPU,RAM,etc) needed matter :-); but if you can don't be dependant on tools. Personnally I think we should be able to code with a vim, notebook or Eclipse, etc.

Here are some Php debuggers :

- Xdebug http://www.xdebug.org/
- http://gubed.mccabe.nu/

And when you use Eclipse+Pdt or Netbeans you have a debugger integrated.

Cheers,

Golchi said...

Take a look at this bog post i wrote http://golchitech.blogspot.com/2009/03/development-tools-are-important.html

Prajwala said...

I checked the php debuggers but they are on the web, not on the shell. I want one debugger on shell. So that to check any logical error I no need to go to browser.

I agree to your points saying try other tools and know the limits of your tools. I like to try new tools.

cheers,
Prajwala