php 在mac上双击运行Php脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19951457/
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 20:16:30  来源:igfitidea点击:

Double click and run the Php script on mac

phphtmlmacos

提问by TryingToLearn

I am sorry if this question was answered.

如果回答了这个问题,我很抱歉。

Why can't I run php code directly without using terminal on mac.What I mean when you double click on html file it automatically opens in the browser but not in the case of php.If I try to double click on php it opens with some text-editor.

为什么我不能在 mac 上不使用终端直接运行 php 代码。我的意思是当你双击 html 文件时,它会自动在浏览器中打开,但在 php 的情况下不会。如果我尝试双击 php,它会打开一些文本编辑器。

Any help would be helpful.

任何帮助都会有所帮助。

回答by SimonSimCity

I think you don't understand what PHP is ...

我想你不明白PHP是什么......

HTML is a markup-language, that can directly be understood by the browser. If the browser opens the file, it can do something with the content.

HTML 是一种标记语言,可以直接被浏览器理解。如果浏览器打开文件,它可以对内容做一些事情。

As PHP is a programming-language, you need a parser. This parser is your PHP executable. This program can understand PHP and does nothing more, than running the code and giving something as result. This result may be an HTML webpage, an image or whatever.

由于 PHP 是一种编程语言,因此您需要一个解析器。这个解析器是你的 PHP 可执行文件。这个程序可以理解 PHP,除了运行代码并给出一些结果之外,什么也做不了。此结果可能是 HTML 网页、图像或其他任何内容。

Since you said, you're using a mac, here's a quick introduction on how to set up your personal webserver:

既然你说,你使用的是 mac,这里有一个关于如何设置你的个人网络服务器的快速介绍:

On Mac OSX, PHP and Apache (that's what I use in this example) is already installed and pre-configured. You can just start using it like this:

在 Mac OSX 上,PHP 和 Apache(这就是我在本示例中使用的)已经安装和预配置。你可以像这样开始使用它:

Go into your system preferences and verify that Web Sharingis enabled.

进入您的系统首选项并确认Web Sharing已启用。

Open the Finder and go to /Library/WebServer/Documents/localhost. All files that are in there are processed by the local webserver (Apache and PHP, if you want to know that). Place your file in there and open your webserver and call http://localhost/YourFile.phpand it will call the file YourFile.phpand show you what the output of the script is.

打开 Finder 并转到/Library/WebServer/Documents/localhost。那里的所有文件都由本地网络服务器(Apache 和 PHP,如果你想知道的话)处理。将您的文件放在那里并打开您的网络服务器并调用http://localhost/YourFile.php它会调用该文件YourFile.php并向您显示脚本的输出是什么。

EDIT:

编辑:

If you are using PHP for scripts, like bash-scripts, see the answer @andreas-baumgart provided.

如果您将 PHP 用于脚本,例如 bash-scripts,请参阅@andreas-baumgart 提供的答案。

回答by ?sh

To run PHP in MAC, one should start the built-in Apache Web server and also enable the PHP already installed.

要在 MAC 中运行 PHP,应启动内置的 Apache Web 服务器并启用已安装的 PHP。

This can be done with the following steps.

这可以通过以下步骤完成。

  1. Go to /etc/apache2/httpd.confand change the permission to sudo chmod 777 httpd.conf

  2. Then open the above file to uncomment the line #LoadModule php5_module libexec/apache2/libphp5.so

  3. To start the apache built-in server, use the command sudo apachectl startin the terminal.

  1. 转到/etc/apache2/httpd.conf并将权限更改为sudo chmod 777 httpd.conf

  2. 然后打开上面的文件取消注释该行 #LoadModule php5_module libexec/apache2/libphp5.so

  3. 要启动 apache 内置服务器,请sudo apachectl start在终端中使用命令。

Now .php files can be created and run from the terminal using php -f filename.phpand it can also be run on a browser using http://localhost/filename.php

现在 .php 文件可以从终端使用创建和运行php -f filename.php,它也可以使用在浏览器上运行http://localhost/filename.php

回答by Andreas Baumgart

You cannot execute plain PHP scripts as they are no executable programs but source code. As such they contain just the receipt for an interpreter to create executable code. In order to run your PHP script you need to pass it to the PHP interpreter. In your scenario you can archive that by providing a shebang.

您不能执行普通的 PHP 脚本,因为它们不是可执行程序而是源代码。因此,它们仅包含解释器创建可执行代码的收据。为了运行您的 PHP 脚本,您需要将它传递给 PHP 解释器。在您的场景中,您可以通过提供shebang将其存档。

To run your script on double click try this:

要双击运行您的脚本,请尝试以下操作:

  1. Make the script executable using chmod +x yourscript.php
  2. Prepending the according Shebang to the files content: #!/usr/bin/env php.
  3. Select a PHP file in Finder, hit CMD-i and change "Open With" to "Terminal.app".
  1. 使脚本可执行使用 chmod +x yourscript.php
  2. 在文件内容之前添加相应的 Shebang:#!/usr/bin/env php.
  3. 在 Finder 中选择一个 PHP 文件,点击 CMD-i 并将“打开方式”更改为“Terminal.app”。

回答by Pierre Fontenelle

Late response, but was looking into doing this for myself, this coming up as one of the results in my searching wanted to provide 2 solutions since I ultimately came to both on my own.

迟到的回应,但我正在考虑为自己做这件事,这是我搜索的结果之一,想提供 2 个解决方案,因为我最终都是自己来的。

Solution #1

解决方案#1

The simple way is to go a round about way by writing a wrapper file to execute the script you're working on. Create a file with the following code:

简单的方法是通过编写一个包装文件来执行您正在处理的脚本来绕过。使用以下代码创建一个文件:

#!/usr/bin/php
<?php

include('name-of-php-script.php');

?>

Save it as wrapper.commandThe name wrapper isn't important, but the command extension tells Finder that this is a shell script to open up in Terminal. The file itself just executes whatever php script is in the include.

将其另存为wrapper.command名称包装器并不重要,但命令扩展名告诉 Finder 这是一个可在终端中打开的 shell 脚本。文件本身只执行包含在包含的任何 php 脚本。

Solution #2

解决方案#2

The specific inquiry requires a bit of work.

具体查询需要做一些工作。

First make sure that the 1st line of the php script is:

首先确保php脚本的第一行是:

#!/usr/bin/php

This is where the preinstalled version of PHP is installed on Mac OS X. You can always verify by running this command in terminal:

这是 Mac OS X 上预装 PHP 版本的安装位置。您始终可以通过在终端中运行此命令来验证:

whereis php

Once you've added the Shebang line to the php script you've readied it for automatic execution.

将 Shebang 行添加到 php 脚本后,您就已经准备好自动执行了。

To make it double clickable executeable you have to do the following: Right click on the PHP script and click Get Info. Click where it says Open With, click the default option to see all the available options. Select Other...

要使其可双击执行,您必须执行以下操作: 右键单击​​ PHP 脚本并单击Get Info。单击“打开方式”的位置,单击默认选项以查看所有可用选项。选择其他...

Switch where it says Enable:from Recommended Applicationsto All Applications, and click the checkbox for Always Open With. Choose Terminal as the application. Finally, you have to click the button that says Change All...

将其显示为Enable:from Recommendation Applications切换到All Applications,然后单击Always Open With复选框。选择终端作为应用程序。最后,您必须单击“全部更改...”按钮

OS X will verify you want it to set Terminal as the default application to open .php files

OS X 将验证您是否希望将终端设置为打开 .php 文件的默认应用程序

This will make every php file open up in terminal by default, but unless they contain the #!/usr/bin/php line they won't actually run.

这将使每个 php 文件默认在终端中打开,但除非它们包含 #!/usr/bin/php 行,否则它们实际上不会运行。

回答by cyber8200

Try MAMP

试试MAMP

MAMP 4 brings even more opportunities for web developers. We are now supporting MySQL 5.6 and Nginx is now fully integrated. Server starting times have been improved.

MAMP 4 为 Web 开发人员带来了更多机会。我们现在支持 MySQL 5.6,Nginx 现在已经完全集成。服务器启动时间已得到改善。

enter image description here

在此处输入图片说明

回答by Thushara Buddhika

Try this (for mac),

试试这个(对于mac),

  1. Open terminal

  2. cdto folder

  3. Start php server - php -S 127.0.0.1:8000

  4. Open browser and enter - http://localhost:8000/file-name.php

  1. 打开 terminal

  2. cd到文件夹

  3. 启动php服务器- php -S 127.0.0.1:8000

  4. 打开浏览器并输入 - http://localhost:8000/file-name.php

回答by Woodstock

Because .php files are not 'executable' per se, instead they are just text files with a PHP extension.

因为 .php 文件本身不是“可执行的”,它们只是带有 PHP 扩展名的文本文件。

You need to run the php interpreter against the file to execute on it's contents.

您需要对文件运行 php 解释器以在其内容上执行。