如何在 Sublime Text 2 中运行 PHP 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25322758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:47:01  来源:igfitidea点击:

How to run PHP code in Sublime Text 2

phpsublimetext2

提问by SidraM

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

我对 Sublime text 2 很陌生。我刚刚开始使用它,对此一无所知。我想在 PHP 上工作。我已经建立了一个系统'php'。选择“php”构建系统后,我正在创建一个新文件并仅使用一个回显行编写 PHP 代码并尝试运行它。每次它都给我这个错误

enter image description here

在此处输入图片说明

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

默认情况下,Sublime Text 2 保存在 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

并且自动保存文件的路径是 C:\Users\SM Ahmed\AppData\Roaming\Sublime Text 2\Packages\User

What should I do that makes my code run succesfully

我该怎么做才能使我的代码成功运行

回答by Jan-Willem de Boer

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

虽然传统上 PHP 脚本由 Web 服务器运行,但可以通过 Sublime 运行它们。要设置网络服务器,我建议您阅读以下内容: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.

它将帮助您开始了解如何设置本地服务器和“运行”PHP。

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

如果你想从你的 Sublime Text 2/3 控制台运行 PHP,你应该去:

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

and then edit the file like this:

然后像这样编辑文件:

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

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

哪里/path/to/php是像/usr/local/bin/php在Linux / OS X或C:/WAMP/bin/php.exeWindows上(请务必使用正斜杠/)。将文件另存为Packages/User/PHP.sublime-build在那里Packages,当你选择的是文件夹中打开Preferences -> Browse Packages...。接下来,单击Tools -> Build System -> PHP并点击Ctrl+B以运行您的脚本(或Cmd+B在 Mac 上)。您应该在打开的构建控制台中看到输出(如果有)。

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

确保您的 PHP中没有任何错误,并确保 PHP配置正确

回答by K-Gun

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;

作为 Ubuntu 用户的参考和示例,如果您需要简单地检查您打开的当前文件中的 PHP 语法,这很有效;

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

创建一个文件/home/$USER/.config/sublime-text-3/Packages/User/PHP - Check Syntax.sublime-build并放入其中。

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