您的位置:首页 > 运维架构 > Linux

Linux 命令行中PHP 常用命令

2018-01-23 15:22 981 查看


                      PHP 常用命令行


1、PHP运行指定文件

php -f  test.php (-f 可省略)


1

2


2、命令行直接运行PHP代码

php -r "phpinfo();"


1

2

如果结果太长,还可以 php -r “phpinfo();” | less 分页展示


3、交互模式运行PHP

简单的运算


 

control + c/z 或者 exit 退出交互模式

函数



上面输出结果中返回的数据类型为 NULL。这个问题可以通过要求 php 交互 shell用 return 代替 echo 返回结果来修复。

永远都记住,用户定义的函数不会从一个shell会话保留到下一个shell会话,因此,一旦你退出交互shell,它就会丢失了。


4、PHP脚本作为shell脚本运行

没有权限则切换到root用户 sudo su
echo '#!/usr/bin/php\n<?php var_dump($argv); ?>' > phpscript


注意,我们在该PHP脚本的第一行使用#!/usr/bin/php,就像在shell脚本中那样(/bin/bash)。第一行的#!/usr/bin/php告诉Linux命令行用 PHP 解释器来解析该脚本文件。 

确定phpscript 有可执行权限
chmod u+x phpscript

./phpscript -h --foo

array(3) {
[0]=>
string(11) "./phpscript"
[1]=>
string(2) "-h"
[2]=>
string(5) "--foo"
}





5、其他常用命令

php -m 内置及Zend加载的模块

php -i 等价于 phpinfo()

php -i | grep php.ini 查看php配置文件加载路径 

php –ini 同上

php -v 查看php版本 

php –version 同上

php –re 查看是否安装相应的扩展 如 php –re gd

更多命令 php –help
============================分割线====================================


在 Linux 命令行中使用和执行 PHP 代码

PHP是HTML的嵌入脚本,它便于开发人员快速写出动态生成的页面。PHP主要用于服务器端(而Javascript则用于客户端)以通过HTTP生成动态网页,然而,当你知道可以在Linux终端中不需要网页浏览器来执行PHP时,你或许会大为惊讶。

本文将阐述PHP脚本语言的命令行方面。

1. 在安装完PHP和Apache2后,我们需要安装PHP命令行解释器。


#
apt-get install php5-cli
[Debian
及类似系统]


#
yum install php-cli
[CentOS
及类似系统]


接下来我们通常要做的是,在
/var/www/html
(这是 Apache2 在大多数发行版中的工作目录)这个位置创建一个内容为 
<?php phpinfo(); ?>
,名为 
infophp.php
 的文件来测试(PHP是否安装正确),执行以下命令即可。


#
echo
'<?php phpinfo(); ?>'
>
/var/www/html/infophp.php


然后,将浏览器访问 http://127.0.0.1/infophp.php ,这将会在网络浏览器中打开该文件。



检查PHP信息

不需要任何浏览器,在Linux终端中也可以获得相同的结果。在Linux命令行中执行
/var/www/html/infophp.php
,如:


#
php -f
/var/www/html/infophp.php




从命令行检查PHP信息

由于输出结果太大,我们可以通过管道将上述输出结果输送给 
less
 命令,这样就可以一次输出一屏了,命令如下:


#
php -f
/var/www/html/infophp.php
|
less




检查所有PHP信息

这里,‘-f‘选项解析并执行命令后跟随的文件。

2. 我们可以直接在Linux命令行使用
phpinfo()
这个十分有价值的调试工具而不需要从文件来调用,只需执行以下命令:


#
php -r
'phpinfo();'




PHP调试工具

这里,‘-r‘ 选项会让PHP代码在Linux终端中不带
<
>
标记直接执行。

3. 以交互模式运行PHP并做一些数学运算。这里,‘-a‘ 选项用于以交互模式运行PHP。


#
php -a




Interactive shell




php
>
echo
2+3;


5


php
>
echo
9-6;


3


php
>
echo
5*4;


20


php
>
echo
12/3;


4


php
>
echo
12/5;


2.4


php
>
echo
2+3-1;


4


php
>
echo
2+3-1*3;


2


php
>
exit


输入 ‘exit‘ 或者按下 ‘ctrl+c‘ 来关闭PHP交互模式。



启用PHP交互模式

4. 你可以仅仅将PHP脚本作为shell脚本来运行。首先,创建在你当前工作目录中创建一个PHP样例脚本。


#
echo
-e
'#!/usr/bin/php\n<?php phpinfo(); ?>'
> phpscript.php


注意,我们在该PHP脚本的第一行使用
#!/usr/bin/php
,就像在shell脚本中那样(
/bin/bash
)。第一行的
#!/usr/bin/php
告诉Linux命令行用
PHP 解释器来解析该脚本文件。

其次,让该脚本可执行:


#
chmod
755 phpscript.php


接着来运行它,


#
./phpscript.php


5. 你可以完全靠自己通过交互shell来创建简单函数,这你一定会被惊到了。下面是循序渐进的指南。

开启PHP交互模式。


#
php -a


创建一个函数,将它命名为 
addition
。同时,声明两个变量 
$a
 和 
$b



php
>
function addition
($a, $b)


使用花括号来在其间为该函数定义规则。


php
>
{


定义规则。这里,该规则讲的是添加这两个变量。


php
{
echo $a
+ $b;


所有规则定义完毕,通过闭合花括号来封装规则。


php
{}


测试函数,添加数字4和3,命令如下:


php
> var_dump
(addition(4,3));



样例输出



7NULL


你可以运行以下代码来执行该函数,你可以测试不同的值,你想来多少次都行。将里头的 a 和 b 替换成你自己的值。


php
> var_dump
(addition(a,b));



php
> var_dump
(addition(9,3.3));



样例输出



12.3NULL




创建PHP函数

你可以一直运行该函数,直至退出交互模式(ctrl+z)。同时,你也应该注意到了,上面输出结果中返回的数据类型为 NULL。这个问题可以通过要求 php 交互 shell用 return 代替 echo 返回结果来修复。

只需要在上面的函数的中 ‘echo‘ 声明用 ‘return‘ 来替换

替换


php
{
echo $a
+ $b;





php
{
return $a
+ $b;


剩下的东西和原理仍然一样。

这里是一个样例,在该样例的输出结果中返回了正确的数据类型。



PHP函数

永远都记住,用户定义的函数不会从一个shell会话保留到下一个shell会话,因此,一旦你退出交互shell,它就会丢失了。

==========================分割线=====================================

This post aims at making you aware of a few awesome features of PHP usage in Linux terminal.

Let us configure a few 
php.ini
 settings
in the PHP interactive shell.

6. Set PHP Command-line Prompt


To set PHP command-line prompt, you need to start a PHP interactive shell from the Linux terminal using following php
-a (enabling PHP Interactive mode) command.
$ php -a


and then set anything (say Hi Tecmint ::) as PHP interactive shell command
prompt, simply as:
php > #cli.prompt=Hi Tecmint ::






Enable PHP Interactive Shell

Also you can set current time as your command Line Prompt, simply as:
php > #cli.prompt=`echo date('H:m:s');` >
22:15:43 >


7. Produce one screen output at a time


In our last article, we have used ‘less‘ command over a lots of places pipelined
with original command. We did this to get one screen of output where output could not fit on one screen. But we can configure php.ini file
to set pager value to less to
produce one screen output at a time simply as,
$ php -aphp > #cli.pager=less






Fix PHP Screen Output

So, next time when you run a command (say debugger 
phpinfo();
)
where the output is too big to fit a screen, it will automatically produce output that fits your current.
php > phpinfo();






PHP Info Output


8. Suggestions and TAB completion


PHP shell is a smart enough to show you suggestions and TAB Completion.
You can use TAB key to use this feature. If more than one option is available for the string that you want to TAB completion, you have to use TAB key twice, else use it once.

In-case of more than one possibility, use TAB twice.
php > ZIP [TAB] [TAB]


In-case of single possibility, use TAB once.
php > #cli.pager [TAB]


You can keep pressing TAB for options till values of option are satisfied.
All the activities are logged to file 
~/.php-history
.

To check your PHP interactive shell activity log, you may run:
$ nano ~/.php_history | less






Check PHP Interactive Shell Logs


9. You can use color inside PHP interactive shell. All you need to know are the color codes.


Use echo to print the output into various colors, simply as:
php > echo “color_code1 TEXT second_color_code”;


or a more explaining example is:
php > echo "\033[0;31m Hi Tecmint \x1B[0m";






Enable Colors in PHP Shell

We have seen till now that pressing the return key means execute the command, however semicolon at the end of each command in Php shell is compulsory.

10. Basename in php shell prints the trailing name component of path


The basename function in php shell prints the trailing name component from a given string containing the path to a file or directory.

basename() example #1 and #2.
php > echo basename("/var/www/html/wp/wp-content/plugins");
php > echo basename("www.tecmint.com/contact-us.html");


The above both examples will output:
plugins
contact-us.html






Print Base Name in PHP


11. You may create a file (say test1.txt) using php interactive shell at your Desktop, simply as

$ touch("/home/avi/Desktop/test1.txt");


We have already seen how fine PHP interactive shell is in Mathematics, Here are a few more examples to stun you.

12. Print the length of a string say tecmint.com using PHP interactive shell


strlen function used to get a length of the given string.
php > echo strlen("tecmint.com");






Print Length String in PHP


13. PHP Interactive shell can sort an array. Yes you heard it right


Declare Variable a and set it’s value to array(7,9,2,5,10).
php > $a=array(7,9,2,5,10);


Sort the numbers in the array.
php > sort($a);


Print numbers of the array in sorted order along with their order. The first one is [0].
php > print_r($a);
Array
(
[0] => 2
[1] => 5
[2] => 7
[3] => 9
[4] => 10
)






Sort Arrays in PHP


14. Get the value of Pi in PHP Interactive Shell

php > echo pi();
3.1415926535898


15. Print the square root of a number say 32

php > echo sqrt(150);
12.247448713916


16. Echo a random number from the range be 0-10

php > echo rand(0, 10);






Get Random Number in PHP


17. Get md5sum and sha1sum for a given string

For example, let’s check the md5sum and sha1sum of a string (say avi)
on php shell and cross check the result with those md5sum and sha1sum generated by bash shell.

php > echo md5(avi);
3fca379b3f0e322b7b7967bfcfb948ad
php > echo sha1(avi);
8f920f22884d6fea9df883843c4a8095a2e5ac6f

$ echo -n avi | md5sum
3fca379b3f0e322b7b7967bfcfb948ad  -
$ echo -n avi | sha1sum
8f920f22884d6fea9df883843c4a8095a2e5ac6f  -






Check md5sum and sha1sum in PHP

This is just a glimpse of what can be achieved from a PHP Shell and how interactive is PHP shell. That’s all for now from me. Keep Connected to tecmint. Provide us with your valuable feedback in the comments. Like and share us to get spread.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息