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

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

问题描述

我是php的新手,想从命令行运行php.我已经安装了WAMP,并将系统变量"设置为我的php文件夹(C:\wamp\bin\php\php5.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:\wamp\bin\php\php5.4.3).

当我转到Run-> CMD->键入php -a并按Enter键时,它说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并按Enter键时,我什至看不到"php>"之类的东西.

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

推荐答案

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

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 scrip,那么它非常简单且非常有用.

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:\wamp\bin\php\phpx.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.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=

像这样调用此命令文件以使用默认版本的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天全站免登陆