How To Run PHP From Windows Command Line in WAMPServer

前端 未结 11 694
予麋鹿
予麋鹿 2020-11-22 04:58

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.

相关标签:
11条回答
  • 2020-11-22 05:29

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

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

    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.

    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:

    ::
    :: 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)

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

    0 讨论(0)
  • 2020-11-22 05:30

    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 :)

    p.s. run the cmd as Administrator!

    0 讨论(0)
  • 2020-11-22 05:33

    In windows, put your php.exe file in windows/system32 or any other system executable folders and then go to command line and type php and hit enter following it, if it doesnt generate any error then you are ready to use PHP on command line. If you have set your php.exe somewhere else than default system folders then you need to set the path of it in the environment variables! You can get there in following path....

    control panel -> System -> Edith the environment variables of your account -> Environment Vaiables -> path -> edit then set the absolute path of your php.exe there and follow the same procedure as in first paragraph, if nothing in the error department, then you are ready to use php from command line!

    0 讨论(0)
  • 2020-11-22 05:38

    Try using batch file

    1. Open notepad
    2. type php -S localhost:8000
    3. save file as .bat extension, server.bat
    4. now click on server.bat file your server is ready on 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"

    0 讨论(0)
  • 2020-11-22 05:39

    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 )

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

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

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

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

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

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

    Then run

    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

    @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=\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

    > phppath
    

    Or to get a specific version of PHP like this

    > phppath 5.6.30
    
    0 讨论(0)
提交回复
热议问题