如何在 Windows 上使用命令行访问 PHP?

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

How to access PHP with the Command Line on Windows?

phpcommand-linecommand-line-interface

提问by JasonDavis

I am trying to learn how to access PHP scripts from the command line (CLI)

我正在尝试学习如何从命令行 (CLI) 访问 PHP 脚本

Below is an image from my attempt, please help. I am running Windows 7

下面是我尝试的图像,请帮助。我正在运行 Windows 7

Cli

命令行

回答by DaveRandom

You need to add your PHP installation directory to the %PATH%environment variable, or work from the PHP installation directory.

您需要将 PHP 安装目录添加到%PATH%环境变量中,或者在 PHP 安装目录下工作。

To add it to path (The best approach - Edited for Windows 7):

将其添加到路径(最佳方法 -针对 Windows 7 编辑):

  • Right-click on a My Computericon
  • Click Properties
  • Click Advanced system settingsfrom the left nav
  • Click Advancedtab
  • Click Environment Variablesbutton
  • In the System Variablessection, select Path(case-insensitive) and click Editbutton
  • Add a semi-colon (;) to the end of the string, then add the full file system path of your PHP installation (e.g. C:\Program Files\PHP)
  • Keep clicking OKetc until all dialog boxes have disappeared
  • Close your command prompt and open it again
  • Sorted
  • 右键单击一个My Computer图标
  • 点击 Properties
  • Advanced system settings从左侧导航单击
  • 单击Advanced选项卡
  • 单击Environment Variables按钮
  • 在该System Variables部分中,选择Path(不区分大小写)并单击Edit按钮
  • ;在字符串末尾添加分号 ( ),然后添加 PHP 安装的完整文件系统路径(例如C:\Program Files\PHP
  • 继续单击OK等直到所有对话框都消失
  • 关闭命令提示符并再次打开它
  • 已排序

Alternatively, you can run cd <PHP installation path>before you try and run you command, or call your script like <FULL file system path of php.exe> <path to script>

或者,您可以cd <PHP installation path>在尝试运行命令之前运行,或者像这样调用脚本<FULL file system path of php.exe> <path to script>

回答by Nabi K.A.Z.

For temporary use, try this: (benefit for use npmor composer)

临时使用,试试这个:(有益于使用npmcomposer

> SET PATH=c:\wamp\bin\php\php5.3.29\;%PATH%

For test use this:

对于测试使用这个:

> echo %PATH%
c:\wamp\bin\php\php5.3.29\;c:\anothers\;

Now you can access to PHPcommand:

现在您可以访问PHP命令:

> php -v
PHP 5.3.29 (cli)

回答by Mikeys4u

Easy Way: Run CMD then type

简单方法:运行 CMD 然后输入

C:\wamp\bin\php\php5.4.3\php.exe -f "C:\wamp\www\timenow.php"

Change the last part for a different script, and make sure you have the same version php folder as I do. (php5.4.3)

将最后一部分更改为不同的脚本,并确保您拥有与我相同版本的 php 文件夹。(php5.4.3)