在 Windows 命令提示符 (cmd) 中运行 .php 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12870880/
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
Run .php file in Windows Command Prompt (cmd)
提问by user1687804
I want to run a .phpfile in windows cmd.
I followed this (suggestion)
php is not recognized as an internal command (in Windows)
我想.php在 Windows cmd 中运行一个文件。我遵循了这个(建议)
php 不被识别为内部命令(在 Windows 中)
But it did not work.
但它没有用。
I tried this command prompt:C:\Windows\system32> cd \myfolder
我试过这个命令提示符:C:\Windows\system32> cd \myfolder
And within myfolderI did as follows:C:..\myfolder> php file.php
but I get an error "php is not recognized as internal or external"
在myfolder我内部我做了如下:C:..\myfolder> php file.php
但我收到一个错误“php不被识别为内部或外部”
But when I try without phpcommand like this;C:..\myfolder> file.php
it just opens in notepad, yet I want to run it in cmd.
How can I do it?
但是当我尝试没有这样的php命令时;C:..\myfolder> file.php
它只是在记事本中打开,但我想在cmd.
我该怎么做?
回答by janenz00
If running Windows 10:
如果运行 Windows 10:
- Open the start menu
- Type
path - Click Edit the system environment variables(usually, it's the top search result) and continue on step 6below.
- 打开开始菜单
- 类型
path - 单击编辑系统环境变量(通常是搜索结果的顶部)并继续下面的第6步。
If on older Windows:
如果在较旧的 Windows 上:
Show Desktop.
Right Click My Computershortcut in the desktop.
Click Properties.
You should see a section of control Panel - Control Panel\System and Security\System.
Click Advanced System Settingson the Left menu.
Click Enviornment Variablestowards the bottom of the System Propertieswindow.
Select PATHin the user variables list.
Append your PHP Path (C:\myfolder\php) to your PATH variable, separated from the already existing string by a semi colon.
Click OK
Open your "cmd"
Type PATH, press enter
Make sure that you see your PHP folder among the list.
显示桌面。
右键单击桌面上的我的电脑快捷方式。
单击属性。
您应该看到控制面板的一部分 - Control Panel\System and Security\System。
单击左侧菜单上的高级系统设置。
单击“系统属性”窗口底部的环境变量。
在用户变量列表中选择PATH。
将您的 PHP 路径 (C:\myfolder\php) 附加到您的 PATH 变量,用分号与现有字符串分隔。
单击确定
打开你的“cmd”
输入PATH,按回车
确保您在列表中看到您的 PHP 文件夹。
That should work.
那应该工作。
Note: Make sure that your PHP folder has the php.exe. It should have the file type CLI. If you do not have the php.exe, go ahead and check the installation guidelines at - http://www.php.net/manual/en/install.windows.manual.php- and download the installation file from there.
注意:确保您的 PHP 文件夹中有 php.exe。它应该具有文件类型 CLI。如果您没有 php.exe,请继续检查安装指南 - http://www.php.net/manual/en/install.windows.manual.php- 并从那里下载安装文件。
回答by user2458523
you can for example: set your environment variable path with php.exe folder e.g c:\program files\php
例如,您可以:使用 php.exe 文件夹设置环境变量路径,例如 c:\program files\php
create a script file in d:\ with filename as a.php
在 d:\ 中创建一个脚本文件,文件名为 a.php
open cmd: go to d: drive using d: command
打开cmd:使用d:命令转到d:驱动器
type following command
输入以下命令
php -f a.php
php -f a.php
you will see the output
你会看到输出
回答by devdRew
回答by Osman Gani Khan Masum
It seems your question is very much older. But I just saw it. I searched(not in google) and found My Answer.
看来你的问题很老了。但我刚看到。我搜索了(不是在谷歌中)并找到了我的答案。
So I am writing its solution so that others may get help from it.
所以我正在编写它的解决方案,以便其他人可以从中获得帮助。
Here is my solution.
这是我的解决方案。
Unlikethe other answers, you don't need to setup environments.
与其他答案不同,您不需要设置环境。
all you need is just to write php index.phpif index.phpis your file name.
所有你需要的只是写php index.php,如果index.php是你的文件名。
then you will see that, the file compiled and showing it's desired output.
然后你会看到,文件被编译并显示它所需的输出。

