WAMPServer의 Windows 명령 줄에서 PHP를 실행하는 방법
저는 PHP를 처음 접했고 명령 줄에서 PHP를 실행하고 싶었습니다. WAMP를 설치하고 "시스템 변수"를 php 폴더 ( C:\wamp\bin\php\php5.4.3
)로 설정했습니다.
내가 갈 때 Run
> - CMD
-> 유형 php -a
및 히트 입력, 그것은 말한다 interactive mode enabled
. 그러나 내가 쓸 때 echo 'Hi';
아무것도 보이지 않습니다.
입력 php -a
하고 엔터 키를 눌렀을 때 'php>'와 같은 것도 보이지 않습니다 .
호출되는 PHP CLI (명령 줄 인터페이스의 경우 php)는 php.exe라고합니다. 여기에 있습니다 c:\wamp\bin\php\php5.x.y\php.exe
(여기서 x와 y는 설치 한 PHP의 버전 번호입니다).
명령 줄에서 실행할 PHP 스크립트를 만들고 싶다면 쉽고 매우 유용합니다.
이와 같은 배치 파일을 직접 만들어서 다음과 같이 호출하십시오 phppath.cmd
.
PATH=%PATH%;c:\wamp\bin\php\phpx.y.z
php -v
x.y.z
WAMPServer에 설치 한 PHP 버전의 유효한 폴더 이름으로 변경하십시오.
이미 PATH에있는 폴더 중 하나에 저장하면 어디서나 실행할 수 있습니다.
이제 명령 창에서 소스 폴더로 cd하고> phppath를 실행합니다.
그런 다음 실행
php your_script.php
꿈처럼 작동해야합니다.
다음은 필요한 경우 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=
기본 버전의 PHP를 사용하려면이 명령 파일을 다음과 같이 호출하십시오.
> phppath
또는 이와 같은 특정 버전의 PHP를 얻으려면
> phppath 5.6.30
몇 년 전에이 문제를 우연히 만났을 때가 기억납니다. Windows에는 readline이 없어서 대화 형 쉘이 없기 때문에 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.
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
Try using batch file
- Open notepad
- type
php -S localhost:8000
- save file as
.bat
extension,server.bat
- now click on
server.bat
file your server is ready onhttp://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"
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 bug submission.
Try running
php -m
And see if "readline" appears in the output.
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:
php file.php
There is also the phpsh project which provides a (better) interactive shell for PHP. However, some people have had trouble running it under Windows (I did not try this myself).
Edit: According to the documentation here, readline
is not supported under Windows:
Note: This extension is not available on Windows platforms.
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
If you want to just run a quick code snippet you can use the -r option:
php -r "echo 'hi';"
-r allows to run code without using script tags <?..?>
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"
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.
The following solution is specifically for wamp environments:
This foxed me for a little while, tried all the other suggestions, $PATH etc even searched the windows registry looking for clues:
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
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
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.
Hope that helps someone
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!
just do these steps if you don't need your old php version:
- 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
this works well
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!
참고URL : https://stackoverflow.com/questions/15597067/how-to-run-php-from-windows-command-line-in-wampserver
'code' 카테고리의 다른 글
도커 실행이 프로그래밍 방식으로 성공했는지 감지하는 방법은 무엇입니까? (0) | 2020.10.12 |
---|---|
onclick 함수에서 'this'참조를 유지하면서 앵커 태그에서 onclick () 이벤트를 프로그래밍 방식으로 호출하려면 어떻게해야합니까? (0) | 2020.10.12 |
Angularjs 단순 파일 다운로드로 인해 라우터가 리디렉션 됨 (0) | 2020.10.12 |
requests.get ()이 반환되지 않는 이유는 무엇입니까? (0) | 2020.10.12 |
유 방향 그래프가 비순환인지 어떻게 확인합니까? (0) | 2020.10.12 |