Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run PHP code in Sublime Text 2

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

enter image description here

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

like image 740
SidraM Avatar asked Aug 15 '14 07:08

SidraM


People also ask

How do I run a program in Sublime Text 2?

To run code in Sublime Text, go to Tools > Build System, and select the language for your code (Sublime comes with support for various languages like Python, Ruby, Bash, and more). Next, press Cmd+B on Mac or Ctrl+B on Windows to run your code.

Can you code PHP in sublime?

Sublime Is only text editor. if you want to run PHP scripts in browser you should have to install xampp. and run it manually by entering localhost / your project name.

How do I run a PHP script?

php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.


1 Answers

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!

like image 57
Jan-Willem de Boer Avatar answered Oct 10 '22 00:10

Jan-Willem de Boer