How to run PHP code in Sublime Text 2

While traditionally PHP scripts are run by web servers, it is possible to run them through Sublime. To set up a webserver, I suggest you to read this: http://www.ntu.edu.sg/home/ehchua/programming/howto/WampServer_HowTo.html

It will get you started on how to setup a local server and 'run' PHP.

If you want to run PHP from your Sublime Text 2/3 console you should go to:

Tools -> Build System -> New Build System... 

and then edit the file like this:

{
    "cmd": ["/path/to/php", "$file"]
}

where /path/to/php is something like /usr/local/bin/php on Linux/OS X or C:/WAMP/bin/php.exe on Windows (make sure to use forward slashes /). Save the file as Packages/User/PHP.sublime-build where Packages is the folder opened when you select Preferences -> Browse Packages.... Next, click on Tools -> Build System -> PHP and hit Ctrl+B to run your script (or Cmd+B on a Mac). You should see the output, if any, in the build console that opens.

Be sure there aren't any errors in your PHP and also be sure that PHP is configured correctly!


As a ref and an example for Ubuntu users, that works well if you need to simply check PHP syntax in current file that you opened;

Create a file in /home/$USER/.config/sublime-text-3/Packages/User/PHP - Check Syntax.sublime-build and put in it.

{
    "cmd": ["php", "-l", "$file"]
}