<X />C{}JS();

Adding a PHP Build System to Sublime Text 2

2012-01-02

Sublime Text 2 Screenshot

An updated version of this configuration file can be found at https://gist.github.com/xcjs/6075914

Sublime Text 2 has become one of my favorite text editors, despite being only beta release software. The interface is very attractive and polished, configuration is very flexible through the use of various JSON files, and it is cross-platform so that my text editing environment is largely identical no matter which platform I am on.

One configurable component of Sublime text 2 is its build system, which is not too different from any other text editor out there that offers similar functionality.

Though I've moved onto .NET professionally, a lot of my hobbyist and individual development is still performed using open source technologies, such as PHP. While Sublime Text 2's syntax highlighting is extremely useful, it is far from being a definitive PHP parser. However, the PHP binary is.

When the PHP binary is passed a file path to a PHP script, it will check the syntax of the file and show any parsing errors as well as the output of the script, if any.

In my example, I've used Windows and the WAMP server environment, though this method should work on any operating system that Sublime Text 2 and PHP support.

First, navigate to "Tools > Build System > New Build System..." in Sublime Text 2.

A new text file will open up in Sublime Text 2 titled "untitled.sublime-build" containing the following text:

{
    "cmd": ["make"]
}

This is the JSON text we will edit to create a new PHP build system. Edit the file to resemble the following JSON string:

{
    "cmd": ["path", "$file"]
}

path is where you enter the path to your PHP binary.

$file is a macro for the current active file in Sublime Text 2. Make sure to escape any characters with a backslash, such as backslash itself () and quotes (").

For example, below is my current configuration for the PHP 5.2.6 build system that I've created:

{
    "cmd": ["C:\\wamp\\bin\\php\\php5.2.6\\php.exe", "$file"]
}

The next step is to save your sublime-build file. Sublime Text 2 should open to the correct directory to save the file. Sublime Text 2 saves all custom scripts in the operating system's user directory so that your entire custom configuration is portable and separated by user. The name of the file minus the .sublime-build extension is what Sublime Text 2 will use as the name of the build system, so make sure it is formatted as you would like it to be.

Unfortunately, you will have to close and reopen Sublime Text 2 in order to use the build system. Once you select your build system for PHP when a PHP file is opened, it will become the default build system for any PHP script you have opened your build system will always be for PHP unless you change it again. Unfortunately, naming the build system "PHP" does not seem to make it work for the automatic setting at this time.

For WAMP users, you can even create separate build systems for different versions of PHP as I've done.

The final step is to test it, so find a PHP file, open it within Sublime Text 2, select your newly created build system, and press Ctrl + B on your keyboard to execute the command you've just defined.

Sublime Text 2 will display console output in the bottom of the editor as a footer, as shown below:

Sublime Text 2 Console Output

Congratulations! You now have PHP syntax checking built into Sublime Text 2 at the press of a button. Do proceed carefully, as the script will actually execute. If your script is a class that contains functions and properties, none of the functions will execute as long as the class isn't instantiated or functions aren't called explicitly.

If you only want to perform a lint check (syntax only) without executing the script for output, pass the PHP binary the -l parameter.

You can exit the Sublime Text 2 console by pressing Esc if you want to recover screen real-estate.