명령 줄에서 PHP 코드 문자열 실행
다음 옵션이 작동하는 방식과 유사한 명령 줄에서 PHP 코드 줄을 실행할 수 있기를 바랍니다.
:~> perl -e "print 'hi';"
:~> python -c "print 'hi'"
:~> ruby -e "puts 'hi'"
나는 할 수 있기를 바란다 :
:~> php "echo 'hi';"
PHP에 필요한 작업을 수행 할 수있는 -r 옵션이 있지만 사용하려고 할 때 사용할 수없는 것 같습니다. PHP 5.2.13과 PHP 4.4.9를 사용해 보았지만 둘 다 -r 옵션을 사용할 수 없습니다.
나는이 스크립트 (내가 run_php.php라고 불렀다)를 썼다. 이것은 작동한다. 그러나 나는 그것을하는 더 "올바른"방법이 있어야한다고 느끼기 때문에 그것을 열렬히 좋아하지 않는다.
#!/usr/bin/php5 -q
<?php echo eval($argv[1]); ?>
내 질문은 : -r 옵션이 있습니까? 그렇다면 --help를 실행할 때 사용할 수없는 이유는 무엇입니까? -r 옵션이없는 경우이를 수행하는 가장 좋은 방법은 무엇입니까 (가능한 경우 중간 스크립트를 작성하지 않고)?
감사!
=== 편집 ===
위에서 명확하지 않다고 생각하기 때문에 -r 옵션을 사용할 수 없습니다. 다음은 내가 실행중인 두 버전의 PHP에 대한 php -h 출력입니다.
PHP 4.4.9
Usage: php [-q] [-h] [-s] [-v] [-i] [-f <file>]
php <file> [args...]
-a Run interactively
-C Do not chdir to the script's directory
-c <path>|<file> Look for php.ini file in this directory
-n No php.ini file will be used
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-f <file> Parse <file>. Implies `-q'
-h This help
-i PHP information
-l Syntax check only (lint)
-m Show compiled in modules
-q Quiet-mode. Suppress HTTP Header output.
-s Display colour syntax highlighted source.
-v Version number
-w Display source with stripped comments and whitespace.
-z <file> Load Zend extension <file>.
PHP 5.2.13
Usage: php [-q] [-h] [-s] [-v] [-i] [-f <file>]
php <file> [args...]
-a Run interactively
-C Do not chdir to the script's directory
-c <path>|<file> Look for php.ini file in this directory
-n No php.ini file will be used
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-f <file> Parse <file>. Implies `-q'
-h This help
-i PHP information
-l Syntax check only (lint)
-m Show compiled in modules
-q Quiet-mode. Suppress HTTP Header output.
-s Display colour syntax highlighted source.
-v Version number
-w Display source with stripped comments and whitespace.
-z <file> Load Zend extension <file>.
-r 옵션이 없습니다. -r 옵션을 사용하려고하면 다음과 같은 결과가 나타납니다.
Error in argument 1, char 2: option not found r
혼란을 드려 죄송합니다.
EDIT2 : 네, PHP 5.2의 cli SAPI에 있습니다.
편집 : 업그레이드 할 수없고 PHP 5.2에 이러한 옵션이없는 경우 (테스트 할 수 없습니다) 다음을 수행 할 수 있습니다.
glopes @ nebm : ~ $ echo "<? php echo \"hi \\ n \ ";" | PHP 안녕하세요
실물:
실제로 -r
옵션이 있습니다 (PHP 5.2에 대해서는 잘 모르겠습니다).
D : \> php -r "echo 'hi';"; 안녕하세요
Just make sure you are using the command line version of PHP. php --version
should give you something like this (notice "cli"):
D:\>php --version PHP 5.3.0 (cli) (built: May 20 2010 19:05:12) (DEBUG) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies
In new versions of PHP you just type "php -a" and you hop into an interactive mode, where you can experiment with PHP.
Extra Semi colon is not required in the end.
You can mention php -r "echo 'hi';"
instead of php -r "echo 'hi';";
Another example(To get current timestamp at command line):
php -r 'print time()."\n";'
The easiest way to do it is to use the -r flag. However, I've found that it does not allow multiline inputs. To work around that, you can do this:
php -r "passthru(file_get_contents('php://stdin'));"
Which lets you pipe from stdin, like so:
echo -n "echo 'test';" | php -r "passthru(file_get_contents('php://stdin'));"
However, to answer the original question, if you do not have the -r flag available, it is also possible to use the -f flag - just pass stdin as the file to open: php -f /dev/stdin
If you do this, be aware that a) you need to include a blank space at the start of the input, and b) you have to open with <?php
. Example:
echo -ne " <?php\necho 'test';" | php -f /dev/stdin
Take a look at this page on PHP command line features, if you haven't already. There are some posts on issues based on OS and double or single quotes.
I would also check the PHP information
php -i
to see if PHP was compiled with CLI support disabled (--disable-cli).
참고URL : https://stackoverflow.com/questions/2954540/execute-a-string-of-php-code-on-the-command-line
'program story' 카테고리의 다른 글
파이썬에 대한 간단한 프로세스 기반 병렬 맵이 있습니까? (0) | 2020.12.08 |
---|---|
jQuery : first 대 .first () (0) | 2020.12.08 |
Haskell 기능 적용 및 커링 (0) | 2020.12.08 |
한 요소의 모든 속성을 복사하여 다른 요소에 적용하는 방법은 무엇입니까? (0) | 2020.12.08 |
IP 주소가 'XXX.XXX.XXX.XX'인 Windows Azure 클라이언트는 서버에 액세스 할 수 없습니다. (0) | 2020.12.08 |