如何在 WAMPServer 中从 Windows 命令行运行 PHP [英] How To Run PHP From Windows Command Line in WAMPServer

查看:45
本文介绍了如何在 WAMPServer 中从 Windows 命令行运行 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:wampinphpphp5.4.3).

当我去 Run -> CMD -> 输入 php -a 并回车时,它说 interactive mode enabled.但是当我写 echo 'Hi'; 它什么也没显示.

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

当我输入 php -a 并按回车键时,我什至看不到任何类似php >"的东西.

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

推荐答案

PHP CLI 被称为 php.exe它位于 c:wampinphpphp5.x.yphp.exe (其中 x 和 y 是您安装的 php 版本号)

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

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

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

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

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

PATH=%PATH%;c:wampinphpphpx.y.z
php -v

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

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

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

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

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

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

然后运行

php your_script.php

php your_script.php

它应该像梦一样工作.

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

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

@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.4.3
set composerInstalled=%baseWamp%composer
set phpFolder=inphpphp

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=%baseWamp%%phpFolder%%phpver%
    set PHP_PEAR_INSTALL_DIR=%baseWamp%%phpFolder%%phpver%pear
    set PHP_PEAR_DOC_DIR=%baseWamp%%phpFolder%%phpver%docs
    set PHP_PEAR_BIN_DIR=%baseWamp%%phpFolder%%phpver%
    set PHP_PEAR_DATA_DIR=%baseWamp%%phpFolder%%phpver%data
    set PHP_PEAR_PHP_BIN=%baseWamp%%phpFolder%%phpver%php.exe
    set PHP_PEAR_TEST_DIR=%baseWamp%%phpFolder%%phpver%	ests

    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=

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

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

> phppath

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

Or to get a specific version of PHP like this

> phppath 5.6.30

这篇关于如何在 WAMPServer 中从 Windows 命令行运行 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆