如何在 WAMPServer 中从 Windows 命令行运行 PHP

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

How To Run PHP From Windows Command Line in WAMPServer

phpwindowsshellcommand-linewamp

提问by Daman

I'm new to php and wanted to run php from command line. I have installed WAMP and set the "System Variables" to my php folder ( which is C:\wamp\bin\php\php5.4.3).

我是 php 新手,想从命令行运行 php。我已经安装了 WAMP 并将“系统变量”设置为我的 php 文件夹(即C:\wamp\bin\php\php5.4.3)。

When i go to Run-> CMD-> Type php -aand hit enter, it says interactive mode enabled. But when I write echo 'Hi';it shows nothing.

当我转到Run-> CMD-> Typephp -a并按 Enter 键时,它会显示interactive mode enabled. 但是当我写echo 'Hi';它时它什么也没显示。

I even don't see anything like 'php >" when i type php -aand hit enter.

当我输入php -a并按回车键时,我什至看不到像“php>”这样的东西。

回答by RiggsFolly

The PHP CLI as its called ( php for the Command Line Interface ) is called php.exe It lives in c:\wamp\bin\php\php5.x.y\php.exe( where x and y are the version numbers of php that you have installed )

PHP CLI 被称为(命令行界面的 php)被称为 php.exe 它存在于c:\wamp\bin\php\php5.x.y\php.exe(其中 x 和 y 是您已安装的 php 版本号)

If you want to create php scrips to run from the command line then great its easy and very useful.

如果您想创建 php 脚本以从命令行运行,那么它非常简单且非常有用。

Create yourself a batch file like this, lets call it phppath.cmd:

为自己创建一个这样的批处理文件,我们称之为phppath.cmd

PATH=%PATH%;c:\wamp\bin\php\phpx.y.z
php -v

Change x.y.zto a valid folder name for a version of PHP that you have installed within WAMPServer

更改x.y.z为您在 WAMPServer 中安装的 PHP 版本的有效文件夹名称

Save this into one of your folders that is already on your PATH, so you can run it from anywhere.

将其保存到 PATH 中已有的文件夹之一,以便您可以从任何地方运行它。

Now from a command window, cd into your source folder and run >phppath.

现在从命令窗口,cd 到您的源文件夹并运行 >phppath。

Then run

然后运行

php your_script.php

php your_script.php

It should work like a dream.

它应该像梦一样工作。

Here is an example that configures PHP Composer and PEAR if required and they exist

这是一个配置 PHP Composer 和 PEAR 的示例(如果需要,它们存在)

@echo off

REM **************************************************************
REM * PLACE This file in a folder that is already on your PATH
REM * Or just put it in your C:\Windows folder as that is on the
REM * Search path by default
REM * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
REM * EDIT THE NEXT 3 Parameters to fit your installed WAMPServer
REM **************************************************************


set baseWamp=D:\wamp
set defaultPHPver=7.1.9
set composerInstalled=%baseWamp%\composer
set phpFolder=\bin\php\php

if %1.==. (
    set phpver=%baseWamp%%phpFolder%%defaultPHPver%
) else (
    set phpver=%baseWamp%%phpFolder%%1
)

PATH=%PATH%;%phpver%
php -v
echo ---------------------------------------------------------------


REM IF PEAR IS INSTALLED IN THIS VERSION OF PHP

IF exist %phpver%\pear (
    set PHP_PEAR_SYSCONF_DIR=D:\wamp\bin\php\php%phpver%
    set PHP_PEAR_INSTALL_DIR=D:\wamp\bin\php\php%phpver%\pear
    set PHP_PEAR_DOC_DIR=D:\wamp\bin\php\php%phpver%\docs
    set PHP_PEAR_BIN_DIR=D:\wamp\bin\php\php%phpver%
    set PHP_PEAR_DATA_DIR=D:\wamp\bin\php\php%phpver%\data
    set PHP_PEAR_PHP_BIN=D:\wamp\bin\php\php%phpver%\php.exe
    set PHP_PEAR_TEST_DIR=D:\wamp\bin\php\php%phpver%\tests

    echo PEAR INCLUDED IN THIS CONFIG
    echo ---------------------------------------------------------------
) else (
    echo PEAR DOES NOT EXIST IN THIS VERSION OF php
    echo ---------------------------------------------------------------
)

REM IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM **************************************************************
REM * IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM *
REM * This assumes that composer is installed in /wamp/composer
REM *
REM **************************************************************
IF EXIST %composerInstalled% (
    ECHO COMPOSER INCLUDED IN THIS CONFIG
    echo ---------------------------------------------------------------
    set COMPOSER_HOME=%baseWamp%\composer
    set COMPOSER_CACHE_DIR=%baseWamp%\composer

    PATH=%PATH%;%baseWamp%\composer

    rem echo TO UPDATE COMPOSER do > composer self-update
    echo ---------------------------------------------------------------
) else (
    echo ---------------------------------------------------------------
    echo COMPOSER IS NOT INSTALLED
    echo ---------------------------------------------------------------
)

set baseWamp=
set defaultPHPver=
set composerInstalled=
set phpFolder=

Call this command file like this to use the default version of PHP

像这样调用这个命令文件以使用默认版本的 PHP

> phppath

Or to get a specific version of PHP like this

或者像这样获得特定版本的 PHP

> phppath 5.6.30

回答by Hendra Uzia

I remember one time when I stumbled upon this issue a few years ago, it's because windows don't have readline, therefore no interactive shell, to use php interactive mode without readline support, you can do this instead:

我记得几年前有一次我偶然发现了这个问题,因为windows没有readline,因此没有交互式shell,要使用没有readline支持的php交互模式,你可以这样做:

C:\>php -a 
Interactive mode enabled 

<?php 
echo "Hello, world!"; 
?> 
^Z 
Hello, world!

After entering interactive mode, type using opening (<?php) and closing (?>) php tag, and end with control Z (^Z) which denotes the end of file.

进入交互模式后,使用打开 ( <?php) 和关闭 ( ?>) php 标签键入,并以控件 Z ( ^Z) 结尾,表示文件结束。

I also recall that I found the solution from php's site user comment: http://www.php.net/manual/en/features.commandline.interactive.php#105729

我还记得我从 php 的站点用户评论中找到了解决方案:http: //www.php.net/manual/en/features.commandline.interactive.php#105729

回答by Nishchit Dhanani

Try using batch file

尝试使用批处理文件

  1. Open notepad
  2. type php -S localhost:8000
  3. save file as .batextension, server.bat
  4. now click on server.batfile your server is ready on http://localhost:8000
  1. 打开记事本
  2. 类型 php -S localhost:8000
  3. 将文件另存为.bat扩展名,server.bat
  4. 现在单击server.bat您的服务器已准备就绪的文件http://localhost:8000

Dependency

依赖

if you got error php not recognize any internal or external command then goto environment variable and edit path to php.exe "C:\wamp\bin\php\php5.4.3"

如果您遇到错误 php 无法识别任何内部或外部命令,则转到环境变量并编辑 php.exe 的路径 "C:\wamp\bin\php\php5.4.3"

回答by driis

The problem you are describing sounds like your version of PHP might be missing the readline PHP module, causing the interactive shell to not work. I base this on this PHP bugsubmission.

您所描述的问题听起来像是您的 PHP 版本可能缺少 readline PHP 模块,从而导致交互式 shell 无法工作。我基于此 PHP 错误提交。

Try running

尝试跑步

php -m

And see if "readline" appears in the output.

并查看“readline”是否出现在输出中。

There might be good reasons for omitting readline from the distribution. PHP is typically executed by a web server; so it is not really need for most use cases. I am sure you can execute PHP code in a file from the command prompt, using:

从发行版中省略 readline 可能有很好的理由。PHP 通常由 Web 服务器执行;所以对于大多数用例来说并不是真正需要的。我相信您可以从命令提示符执行文件中的 PHP 代码,使用:

php file.php

There is also the phpshproject which provides a (better) interactive shell for PHP. However, some people have had trouble running it under Windows (I did not try this myself).

还有phpsh项目,它为 PHP 提供了一个(更好的)交互式 shell。但是,有些人在 Windows 下运行它时遇到问题(我自己没有尝试过)。

Edit: According to the documentation here, readlineis not supported under Windows:

编辑:根据此处的文档readline在 Windows 下不支持:

Note: This extension is not available on Windows platforms.

注意:此扩展在 Windows 平台上不可用。

So, if that is correct, your options are:

所以,如果这是正确的,你的选择是:

  • Avoid the interactive shell, and just execute PHP code in files from the command line - this should work well
  • Try getting phpsh to work under Windows
  • 避免交互式 shell,只需从命令行执行文件中的 PHP 代码 - 这应该很有效
  • 尝试让 phpsh 在 Windows 下工作

回答by James Lock

If you want to just run a quick code snippet you can use the -r option:

如果你只想运行一个快速的代码片段,你可以使用 -r 选项:

php -r "echo 'hi';"

-r allows to run code without using script tags <?..?>

-r 允许在不使用脚本标签的情况下运行代码 <?..?>

回答by Adidi

You can run php pages using php.exe create some php file with php code and in the cmd write "[PATH to php.ext]\php.exe [path_to_file]\file.php"

您可以使用 php.exe 运行 php 页面,使用 php 代码创建一些 php 文件并在 cmd 中写入 "[PATH to php.ext]\php.exe [path_to_file]\file.php"

回答by m3nda

UPDATEDAfter few research, best solution was to use that info another stackoverflow threadto avoid ctrl+z input and also from the scree output. So, instead of php -ayou should use call "php.exe" -f NAMED_SCRIPT.php

更新经过一些研究,最好的解决方案是使用该信息另一个 stackoverflow 线程来避免 ctrl+z 输入以及 scree 输出。所以,而不是php -a你应该使用call "php.exe" -f NAMED_SCRIPT.php

OLDReadline not possible under Windows, so none of existent php shells written in php will work. But there's a workaround using -a interactive mode.

旧的Readline 在 Windows 下是不可能的,所以用 php 编写的现有 php shell 都不能工作。但是有一个使用 -a 交互模式的解决方法。

2 commmon problems here. You cannot see result until executes CTRL Z command to indicate the final of code/file like EOF. When you do, result in most cases is printed result and fast closed window. Anyway, you will be returned to cmd not the -a interactive mode.

这里有2个常见问题。在执行 CTRL Z 命令以指示代码/文件的结尾(如 EOF)之前,您无法看到结果。当你这样做时,结果在大多数情况下是打印结果和快速关闭的窗口。无论如何,您将返回到 cmd 而不是 -a 交互模式。

Save this content into a .bat file, and define your PHP PATH into Windows variables, or modify php.exe to "full path to exe" instead:

将此内容保存到 .bat 文件中,并将 PHP PATH 定义为 Windows 变量,或者将 php.exe 修改为“exe 的完整路径”:

::
:: PHP Shell launch wrapper
::
@ECHO off
call "php.exe" -a

echo.
echo.

call "PHP Shell.bat"

This is a simple Batch launching -a mode of php.exe. When it launchs php, stop script even no pause is wrote because is "into" the interactive waiting for input. When you hit CTRL Z, gets the SIGSTEP (next step) not the SIGSTOP (close, CTRL+C usually), then read the next intruction, wich is a recursive call to .bat itself. Because you're always into PHP -a mode, no exit command. You must use CTRL+C or hit the exit cross with mouse. (No alt+f4)

这是一个简单的批量启动 - php.exe 模式。当它启动 php 时,停止脚本甚至没有写暂停,因为它“进入”交互式等待输入。当您按 CTRL Z 时,获取 SIGSTEP(下一步)而不是 SIGSTOP(通常是关闭,CTRL+C),然后阅读下一个指令,这是对 .bat 本身的递归调用。因为你总是进入 PHP -a 模式,没有退出命令。您必须使用 CTRL+C 或用鼠标点击退出十字。(没有 alt+f4)

You can also use "Bat to Exe" converter to easy use.

您还可以使用“Bat to Exe”转换器来轻松使用。

回答by saan

That is because you are in 'Interactive Mode' where php evaluates everything you type. To see the end result, you do 'ctrl+z' and Enter. You should see the evaluated result now :)

那是因为您处于“交互模式”,php 会评估您输入的所有内容。要查看最终结果,请执行 'ctrl+z' 和 Enter。您现在应该看到评估结果:)

p.s. run the cmd as Administrator!

ps 以管理员身份运行cmd!

回答by BuddhaLincoln

The following solution is specifically for wamp environments:

以下解决方案专门用于 wamp 环境:

This foxed me for a little while, tried all the other suggestions, $PATH etc even searched the windows registry looking for clues:

这让我困惑了一段时间,尝试了所有其他建议,$PATH 等甚至搜索了 Windows 注册表以寻找线索:

The GUI (wampmanager) indicates I have version 7 selected and yes if I phpinfo() in a page in the browser it will tell me its version 7.x.x yet php -v in the command prompt reports a 5.x.x

GUI(wampmanager)指示我选择了版本7,如果我在浏览器的页面中使用phpinfo(),它会告诉我它的版本7.xx,但命令提示符中的php -v报告5.xx

If you right click on the wampmanager head to icon->tools->delete unused versions and remove the old version, let it restart the services then the command prompt will return a 7.x.x

如果您右键单击 wampmanager 头图标->工具->删除未使用的版本并删除旧版本,让它重新启动服务,然后命令提示符将返回 7.xx

This solution means you no longer have the old version if you want to switch between php versions but there is a configuration file in C:\wamp64\wampmanager.conf which appears to specify the version to use with CLI (the parameter is called phpCliVersion). I changed it, restarted the server ... thought I had solved it but no effect perhaps I was a little impatient so I have a feeling there may be some mileage in that.

这个解决方案意味着如果你想在 php 版本之间切换,你不再拥有旧版本,但在 C:\wamp64\wampmanager.conf 中有一个配置文件,它似乎指定了与 CLI 一起使用的版本(该参数称为 phpCliVersion) . 我更改了它,重新启动了服务器......以为我已经解决了它但没有效果也许我有点不耐烦所以我觉得可能会有一些里程。

Hope that helps someone

希望能帮助某人

回答by Mohammad Hadi

just do these steps if you don't need your old php version:

如果您不需要旧的 php 版本,只需执行以下步骤:

  • open wamp and right click on wamp manager than go : tools/Change PHP CLI Version than change php version to latest
  • another time right click on wamp manager than go : tools/Delete unuserd versions and delete the oldest version which your system insist on it to be your pc php version :D
  • go to control panel/user account/change my environment variables and in PATH variable click edit and add your latest php version path which is in your wamp server bin folder
  • close all command lines or IDEs and restart them and check for php -v
  • 打开 wamp 并右键单击 wamp 管理器然后转到:工具/更改 PHP CLI 版本而不是将 php 版本更改为最新
  • 另一次右键单击 wamp 管理器而不是 go:工具/删除未使用的版本并删除您的系统坚持将其作为您的 pc php 版本的最旧版本:D
  • 转到控制面板/用户帐户/更改我的环境变量,然后在 PATH 变量中单击编辑并添加位于 wamp 服务器 bin 文件夹中的最新 php 版本路径
  • 关闭所有命令行或 IDE 并重新启动它们并检查 php -v

this works well

这很好用