How to run PHP code in Sublime Text 2

2019-01-18 00:03发布

问题:

I am very new to Sublime text 2. I have just started using this and know nothing about it. I want to work on PHP. I have built a system 'php'. After choosing 'php' build system I am creating a new file and writing PHP code with just a single echo line and trying to run it. Everytime it is giving my this error

By default Sublime Text 2 saved in C:\Program Files\Sublime Text 2

and the path where automatically files are being saved is C:\Users\SM Ahmed\AppData\Roaming\Sublime Text 2\Packages\User

What should I do that makes my code run succesfully

回答1:

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!



回答2:

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"]
}