[Translated]20150722 12 Useful PHP Commandline Usage Every Linux User Must Know.md

This commit is contained in:
GOLinux 2015-07-24 10:51:47 +08:00
parent ac6dcd4805
commit 4670ba75bd
2 changed files with 193 additions and 204 deletions

View File

@ -1,204 +0,0 @@
Translating by GOLinu!
12 Useful PHP Commandline Usage Every Linux User Must Know
================================================================================
In my last post “[How to Use and Execute PHP Codes in Linux Command line][1]”, I emphasized on running PHP codes directly in Linux Command-line as well as executing PHP script file in Linux Terminal.
![Run PHP Codes in Linux Commandline](http://www.tecmint.com/wp-content/uploads/2015/07/Run-PHP-Codes-in-Linux-Commandline.jpeg)
Run PHP Codes in Linux Commandline Part 2
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](http://www.tecmint.com/wp-content/uploads/2015/07/Enable-PHP-Interactive-Shell.png)
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 -a
php > #cli.pager=less
![Fix PHP Screen Output](http://www.tecmint.com/wp-content/uploads/2015/07/Fix-PHP-Screen-Output.png)
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](http://www.tecmint.com/wp-content/uploads/2015/07/PHP-Info-Output.png)
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](http://www.tecmint.com/wp-content/uploads/2015/07/Check-PHP-Interactive-Shell-Logs.png)
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](http://www.tecmint.com/wp-content/uploads/2015/07/Enable-Colors-in-PHP-Shell.png)
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](http://www.tecmint.com/wp-content/uploads/2015/07/Print-Base-Name-in-PHP.png)
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](http://www.tecmint.com/wp-content/uploads/2015/07/Print-Length-String-in-PHP.png)
Print Length String in PHP
**13. PHP Interactive shell can sort an array. Yes you heard it right**
Declare Variable a and set its 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](http://www.tecmint.com/wp-content/uploads/2015/07/Sort-Arrays-in-PHP.png)
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](http://www.tecmint.com/wp-content/uploads/2015/07/Get-Random-Number-in-PHP.png)
Get Random Number in PHP
**17. Get md5sum and sha1sum for a given string For example, lets 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](http://www.tecmint.com/wp-content/uploads/2015/07/Check-md5sum-and-sha1sum.png)
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. Thats 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.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/execute-php-codes-functions-in-linux-commandline/
作者:[Avishek Kumar][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/avishek/
[1]:http://www.tecmint.com/run-php-codes-from-linux-commandline/

View File

@ -0,0 +1,193 @@
每个Linux人应知应会的12个有用的PHP命令行用法
================================================================================
在我上一篇文章“[在Linux命令行中使用并执行PHP代码][1]”中我同时着重讨论了直接在Linux命令行中运行PHP代码以及在Linux终端中执行PHP脚本文件。
![Run PHP Codes in Linux Commandline](http://www.tecmint.com/wp-content/uploads/2015/07/Run-PHP-Codes-in-Linux-Commandline.jpeg)
在Linux命令行运行PHP代码——第二部分
本文旨在让你了解一些相当不错的Linux终端中的PHP用法特性。
让我们先在PHP交互shell中来对`php.ini`设置进行一些配置吧。
**6. 设置PHP命令行提示符**
要设置PHP命令行提示你需要在Linux终端中使用下面的php -a启用PHP交互模式命令开启一个PHP交互shell。
$ php -a
然后设置任何东西比如说Hi Tecmint ::作为PHP交互shell的命令提示符操作如下
php > #cli.prompt=Hi Tecmint ::
![Enable PHP Interactive Shell](http://www.tecmint.com/wp-content/uploads/2015/07/Enable-PHP-Interactive-Shell.png)
启用PHP交互Shell
同时,你也可以设置当前时间作为你的命令行提示符,操作如下:
php > #cli.prompt=`echo date('H:m:s');` >
22:15:43 >
**7. 每次输出一屏**
在我们上一篇文章中我们已经在原始命令中通过管道在很多地方使用了less命令。通过该操作我们可以在那些不能一次满屏输出的地方获得每次一屏的输出。但是我们可以通过配置php.ini文件设置pager的值为less以每次输出一屏操作如下
$ php -a
php > #cli.pager=less
![Fix PHP Screen Output](http://www.tecmint.com/wp-content/uploads/2015/07/Fix-PHP-Screen-Output.png)
固定PHP屏幕输出
这样,下次当你运行一个命令(比如说条调试器`phpinfo();`)的时候,而该命令的输出内容又太过庞大而不能固定在一屏,它就会自动产生适合你当前屏幕的输出结果。
php > phpinfo();
![PHP Info Output](http://www.tecmint.com/wp-content/uploads/2015/07/PHP-Info-Output.png)
PHP信息输出
**8. 建议和TAB补全**
PHP shell足够智能它可以显示给你建议和进行TAB补全你可以通过TAB键来使用该功能。如果对于你想要用TAB补全的字符串而言有多个选项那么你需要使用两次TAB键来完成其它情况则使用一次即可。
如果有超过一个的可能性请使用两次TAB键。
php > ZIP [TAB] [TAB]
如果只有一个可能性只要使用一次TAB键。
php > #cli.pager [TAB]
你可以一直按TAB键来获得选项直到选项值满足要求。所有的行为都将记录到`~/.php-history`文件。
要检查你的PHP交互shell活动日志你可以执行
$ nano ~/.php_history | less
![Check PHP Interactive Shell Logs](http://www.tecmint.com/wp-content/uploads/2015/07/Check-PHP-Interactive-Shell-Logs.png)
检查PHP交互Shell日志
**9. 你可以在PHP交互shell中使用颜色你所需要知道的仅仅是颜色代码。**
使用echo来打印各种颜色的输出结果看我信手拈来
php > echo “color_code1 TEXT second_color_code”;
一个更能说明问题的例子是:
php > echo "\033[0;31m Hi Tecmint \x1B[0m";
![Enable Colors in PHP Shell](http://www.tecmint.com/wp-content/uploads/2015/07/Enable-Colors-in-PHP-Shell.png)
在PHP Shell中启用彩色
到目前为止我们已经看到按回车键意味着执行命令然而PHP Shell中各个命令结尾的分号是必须的。
**10. PHP shell中的用以打印后续组件的路径名称**
PHP shell中的basename函数从给出的包含有到文件或目录路径的后续组件的路径名称。
basename()样例#1和#2。
php > echo basename("/var/www/html/wp/wp-content/plugins");
php > echo basename("www.tecmint.com/contact-us.html");
上述两个样例都将输出:
plugins
contact-us.html
![Print Base Name in PHP](http://www.tecmint.com/wp-content/uploads/2015/07/Print-Base-Name-in-PHP.png)
在PHP中打印基本名称
**11. 你可以使用PHP交互shell在你的桌面创建文件比如说test1.txt就像下面这么简单**
$ touch("/home/avi/Desktop/test1.txt");
我们已经见识了PHP交互shell在数学运算中有多优秀这里还有更多一些例子会令你吃惊。
**12. 使用PHP交互shell打印比如像tecmint.com这样的字符串的长度**
strlen函数用于获取指定字符串的长度。
php > echo strlen("tecmint.com");
![Print Length String in PHP](http://www.tecmint.com/wp-content/uploads/2015/07/Print-Length-String-in-PHP.png)
在PHP中打印字符串长度
**13. PHP交互shell可以对数组排序是的你没听错**
声明变量a并将其值设置为array(7,9,2,5,10)。
php > $a=array(7,9,2,5,10);
对数组中的数字进行排序。
php > sort($a);
以排序后的顺序打印数组中的数字,同时打印序号,第一个为[0]。
php > print_r($a);
Array
(
[0] => 2
[1] => 5
[2] => 7
[3] => 9
[4] => 10
)
![Sort Arrays in PHP](http://www.tecmint.com/wp-content/uploads/2015/07/Sort-Arrays-in-PHP.png)
在PHP中对数组排序
**14. 在PHP交互Shell中获取Pi的值**
php > echo pi();
3.1415926535898
**15. 打印某个数比如32的平方根**
php > echo sqrt(150);
12.247448713916
**16. 从0-10的范围内回显一个随机数**
php > echo rand(0, 10);
![Get Random Number in PHP](http://www.tecmint.com/wp-content/uploads/2015/07/Get-Random-Number-in-PHP.png)
在PHP中获取随机数
**17. 获取某个指定字符串的md5sum和sha1sum例如让我们在PHP Shell中检查某个字符串比如说avi的md5sum和sha1sum并交叉检查这些带有bash shell生成的md5sum和sha1sum的结果。**
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](http://www.tecmint.com/wp-content/uploads/2015/07/Check-md5sum-and-sha1sum.png)
在PHP中检查md5sum和sha1sum
这里只是PHP Shell中所能获取的功能和PHP Shell的交互特性的惊鸿一瞥这些就是到现在为止我所讨论的一切。保持和tecmint的连线在评论中为我们提供你有价值的反馈吧。为我们点赞并分享帮助我们扩散哦。
--------------------------------------------------------------------------------
via: http://www.tecmint.com/execute-php-codes-functions-in-linux-commandline/
作者:[Avishek Kumar][a]
译者:[GOLinux](https://github.com/GOLinux)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/avishek/
[1]:http://www.tecmint.com/run-php-codes-from-linux-commandline/