简体   繁体   English

如何从命令行执行 PHP 代码?

[英]How can I execute PHP code from the command line?

I would like to execute a single PHP statement like if(function_exists("my_func")) echo 'function exists';我想执行一个 PHP 语句,如if(function_exists("my_func")) echo 'function exists'; directly with the command line without having to use a separate PHP file.直接使用命令行,而无需使用单独的 PHP 文件。

How is it possible?这怎么可能?

If you're going to do PHP in the command line, I recommend you install phpsh, a decent PHP shell .如果您打算在命令行中执行 PHP,我建议您安装phpsh,一个不错的 PHP shell It's a lot more fun.这更有趣。

Anyway, the php command offers two switches to execute code from the command line :无论如何, php命令提供了两个开关来从命令行执行代码

-r <code>        Run PHP <code> without using script tags <?..?>
-R <code>        Run PHP <code> for every input line

You can use php 's -r switch as such:您可以像这样使用php-r 开关

php -r 'echo function_exists("foo") ? "yes" : "no";'

The above PHP command above should output no and returns 0 as you can see:如您所见,上面的 PHP 命令应该输出no返回0

>>> php -r 'echo function_exists("foo") ? "yes" : "no";'
no
>>> echo $? # print the return value of the previous command
0

Another funny switch is php -a :另一个有趣的开关是php -a

-a               Run as interactive shell

It's sort of lame compared to phpsh , but if you don't want to install the awesome interactive shell for PHP made by Facebook to get tab completion, history, and so on , then use -a as such :phpsh相比,有点蹩脚,但是如果您不想安装Facebook 制作的令人敬畏的 PHP 交互式 shell 来获取选项卡完成、历史记录等,那么请使用 -a

>>> php -a
Interactive shell

php > echo function_exists("foo") ? "yes" : "no";
no
php >

If it doesn't work on your box like on my box es ( tested on Ubuntu and Arch Linux ), then probably your PHP setup is fuzzy or broken .如果它不能像我的 box es那样在你的 box 上工作(在UbuntuArch Linux测试),那么可能你的 PHP 设置是模糊的或损坏的 If you run this command:如果您运行此命令:

php -i | grep 'API'

You should see:应该看到:

Server API => Command Line Interface

If you don't , this means that maybe another command will provides the CLI SAPI .如果你不这样做,这意味着也许另一个命令将提供 CLI SAPI Try php-cli;试试 php-cli; maybe it's a package or a command available in your OS.也许它是您的操作系统中可用的包或命令。

If you do see that your php command uses the CLI (command-line interface) SAPI (Server API), then run php -h | grep code如果您确实看到您的php命令使用 CLI(命令行界面)SAPI(服务器 API),则运行php -h | grep code php -h | grep code to find out which crazy switch - as this hasn't changed for year- allows to run code in your version/setup. php -h | grep code找出哪个疯狂的开关——因为这已经一年没有改变了——允许在你的版本/设置中运行代码。

Another couple of examples, just to make sure it works on my boxes:再举几个例子,只是为了确保它适用于我的盒子:

>>> php -r 'echo function_exists("sg_load") ? "yes" : "no";'
no
>>> php -r 'echo function_exists("print_r") ? "yes" : "no";'
yes

Also, note that it is possible that an extension is loaded in the CLI and not in the CGI or Apache SAPI.另请注意,扩展可能加载到 CLI 而不是 CGI 或 Apache SAPI 中。 It is likely that several PHP SAPIs use different php.ini files , eg, /etc/php/cli/php.ini vs. /etc/php/cgi/php.ini vs. /etc/php/apache/php.ini on a Gentoo Linux box.很可能几个 PHP SAPI 使用不同的 php.ini 文件,例如/etc/php/cli/php.ini/etc/php/cgi/php.ini/etc/php/apache/php.iniGentoo Linux 机器上。 Find out which ini file is used with php -i | grep ini找出哪个ini文件与php -i | grep ini php -i | grep ini . php -i | grep ini

Using PHP from the command line从命令行使用 PHP

Use " instead of ' on Windows when using the CLI version with -r :使用带有-r的 CLI 版本时,在 Windows 上使用"而不是'

Correct正确的

php -r "echo 1;"

Incorrect不正确

php -r 'echo 1;'
  PHP Parse error:  syntax error, unexpected ''echo' (T_ENCAPSED_AND_WHITESPACE), expecting end of file in Command line code on line 1

Don't forget the semicolon to close the line (otherwise, the result is "PHP Parse error: syntax error, unexpected end of file, expecting ';' or ',' in Command line code on line 1" ).不要忘记关闭该行的分号(否则,结果是“PHP 解析错误:语法错误,文件意外结束,在第 1 行的命令行代码中需要 ';' 或 ','” )。

You can use:您可以使用:

 echo '<?php if(function_exists("my_func")) echo "function exists"; ' | php

The short tag " < ?= " can be helpful too:短标签“ < ?= ”也很有帮助:

 echo '<?= function_exists("foo") ? "yes" : "no";' | php
 echo '<?= 8+7+9 ;' | php

The closing tag "?>" is optional, but don't forget the final ";"!结束标记“?>”是可选的,但不要忘记最后的“;”!

On the command line:在命令行上:

php -i | grep sourceguardian

If it's there, then you'll get some text.如果它在那里,那么你会得到一些文本。 If not, you won't get a thing.如果没有,你就什么也得不到。

If you're using Laravel , you can use php artisan tinker to get an amazing interactive shell to interact with your Laravel app.如果你使用Laravel ,你可以使用php artisan tinker来获得一个惊人的交互式 shell 来与你的 Laravel 应用程序进行交互。 However, Tinker works with "Psysh" under the hood which is a popular PHP REPL and you can use it even if you're not using Laravel (bare PHP):但是,Tinker 在底层使用“Psysh”,这是一种流行的PHP REPL,即使您不使用 Laravel (裸 PHP),您也可以使用它

// Bare PHP:
>>> preg_match("/hell/", "hello");
=> 1

// Laravel Stuff:
>>> Str::slug("How to get the job done?!!?!", "_");
=> "how_to_get_the_job_done"

One great feature I really like about Psysh is that it provides a quick way for directly looking up the PHP documentation from the command line.我非常喜欢Psysh 的一个很棒的功能是它提供了一种从命令行直接查找 PHP 文档的快速方法。 To get it to work, you only have to take the following simple steps:要使其正常工作,您只需执行以下简单步骤:

apt install php-sqlite3

And then get the required PHP documentation database and move it to the proper location:然后获取所需的 PHP 文档数据库并将其移动到适当的位置:

wget http://psysh.org/manual/en/php_manual.sqlite
mkdir -p /usr/local/share/psysh/ && mv php_manual.sqlite /usr/local/share/psysh/

Now for instance:现在例如:

在此处输入图片说明

To run a PHP shell through a Windows command:要通过 Windows 命令运行 PHP shell:

php -a

It will open the below PHP shell in Windows, where I was testing a simple square_val() function:它将在 Windows 中打开以下 PHP shell,我在其中测试了一个简单的square_val()函数:

function square_val($num, $pow){
  return $num**$pow;
}

The above code was tested.上面的代码经过测试。

To test that the above code works, use echo square_val(4, 2);要测试上述代码是否有效,请使用echo square_val(4, 2); and then echo square_val(4, 0.5);然后echo square_val(4, 0.5); . .

php-shell-命令行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM