[Translated]20150722 How to Use and Execute PHP Codes in Linux Command Line--Part 1.md

This commit is contained in:
joeren 2015-07-23 12:06:33 +08:00
parent 8a7c166f3f
commit 38abc4e6d6
2 changed files with 174 additions and 184 deletions

View File

@ -1,184 +0,0 @@
Translatign by GOLinux!
How to Use and Execute PHP Codes in Linux Command Line Part 1
================================================================================
PHP is an open source server side scripting Language which originally stood for Personal Home Page now stands for PHP: Hypertext Preprocessor, which is a recursive acronym. It is a cross platform scripting language which is highly influenced by C, C++ and Java.
![Run PHP Codes in Linux Command Line](http://www.tecmint.com/wp-content/uploads/2015/07/php-command-line-usage.jpeg)
Run PHP Codes in Linux Command Line Part 1
A PHP Syntax is very similar to Syntax in C, Java and Perl Programming Language with a few PHP-specific feature. PHP is used by some 260 Million websites, as of now. The current stable release is PHP Version 5.6.10.
PHP is HTML embedded script which facilitates developers to write dynamically generated pages quickly. PHP is primarily used on Server-side (and JavaScript on Client Side) to generate dynamic web pages over HTTP, however you will be surprised to know that you can execute a PHP in a Linux Terminal without the need of a web browser.
This article aims at throwing light on the command-line aspect of PHP scripting Language.
**1. After PHP and Apache2 installation, we need to install PHP command Line Interpreter.**
# apt-get install php5-cli [Debian and alike System)
# yum install php-cli [CentOS and alike System)
Next thing, we do is to test a php (if installed correctly or not) commonly as by creating a file `infophp.php` at location /var/www/html (Apache2 working directory in most of the distros), with the content `<?php phpinfo(); ?>`, simply by running the below command.
# echo '<?php phpinfo(); ?>' > /var/www/html/infophp.php
and then point your browser to http://127.0.0.1/infophp.php which opens this file in web browser.
![Check PHP Info](http://www.tecmint.com/wp-content/uploads/2015/07/Check-PHP-Info.png)
Check PHP Info
Same results can be obtained from the Linux terminal without the need of any browser. Run the PHP file located at /var/www/html/infophp.php in Linux Command Line as:
# php -f /var/www/html/infophp.php
![Check PHP info from Commandline](http://www.tecmint.com/wp-content/uploads/2015/07/Check-PHP-info-from-Commandline.png)
Check PHP info from Commandline
Since the output is too big we can pipeline the above output with less command to get one screen output at a time, simply as:
# php -f /var/www/html/infophp.php | less
![Check All PHP Info](http://www.tecmint.com/wp-content/uploads/2015/07/Check-All-PHP-Info.png)
Check All PHP Info
Here Option -f parse and execute the file that follows the command.
**2. We can use `phpinfo()` which is a very valuable debugging tool directly on the Linux command-line without the need of calling it from a file, simply as:**
# php -r 'phpinfo();'
![PHP Debugging Tool](http://www.tecmint.com/wp-content/uploads/2015/07/PHP-Debugging-Tool.png)
PHP Debugging Tool
Here the option -r run the PHP Code in the Linux Terminal directly without tags `<` and `>`.
**3. Run PHP in Interactive mode and do some mathematics. Here option -a is for running PHP in Interactive Mode.**
# 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
Press exit or ctrl+c to close PHP interactive mode.
![Enable PHP Interactive Mode](http://www.tecmint.com/wp-content/uploads/2015/07/Enable-PHP-interactive-mode1.png)
Enable PHP Interactive Mode
**4. You can run a PHP script simply as, if it is a shell script. First Create a PHP sample script in your current working directory.**
# echo -e '#!/usr/bin/php\n<?php phpinfo(); ?>' > phpscript.php
Notice we used #!/usr/bin/php in the first line of this PHP script as we use to do in shell script (/bin/bash). The first line #!/usr/bin/php tells the Linux Command-Line to parse this script file to PHP Interpreter.
Second make it executable as:
# chmod 755 phpscript.php
and run it as,
# ./phpscript.php
**5. You will be surprised to know you can create simple functions all by yourself using the interactive shell. Here is the step-by step instruction.**
Start PHP interactive mode.
# php -a
Create a function and name it addition. Also declare two variables $a and $b.
php > function addition ($a, $b)
Use curly braces to define rules in between them for this function.
php > {
Define Rule(s). Here the rule say to add the two variables.
php { echo $a + $b;
All rules defined. Enclose rules by closing curly braces.
php {}
Test function and add digits 4 and 3 simply as :
php > var_dump (addition(4,3));
#### Sample Output ####
7NULL
You may run the below code to execute the function, as many times as you want with different values. Replace a and b with values of yours.
php > var_dump (addition(a,b));
----------
php > var_dump (addition(9,3.3));
#### Sample Output ####
12.3NULL
![Create PHP Functions](http://www.tecmint.com/wp-content/uploads/2015/07/Create-PHP-Functions.png)
Create PHP Functions
You may run this function till you quit interactive mode (Ctrl+z). Also you would have noticed that in the above output the data type returned is NULL. This can be fixed by asking php interactive shell to return in place of echo.
Simply replace the echo statement in the above function with return
Replace
php { echo $a + $b;
with
php { return $a + $b;
and rest of the things and principles remain same.
Here is an Example, which returns appropriate data-type in the output.
![PHP Functions](http://www.tecmint.com/wp-content/uploads/2015/07/PHP-Functions.png)
PHP Functions
Always Remember, user defined functions are not saved in history from shell session to shell session, hence once you exit the interactive shell, it is lost.
Hope you liked this session. Keep Connected for more such posts. Stay Tuned and Healthy. Provide us with your valuable feedback in the comments. Like ans share us and help us get spread.
Read Also: [12 Useful PHP Commandline Usage on Linux Terminal Part 2][1]
--------------------------------------------------------------------------------
via: http://www.tecmint.com/run-php-codes-from-linux-commandline/
作者:[vishek 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/execute-php-codes-functions-in-linux-commandline/

View File

@ -0,0 +1,174 @@
Linux命令行中使用和执行PHP代码——第一部分
================================================================================
PHP是一个开元服务器端脚本语言最初这三个字母代表的是“Personal Home Page”而现在则代表的是“PHPHypertext Preprocessor”它是个递归首字母缩写。它是一个跨平台脚本语言深受C、C++和Java的影响。
![Run PHP Codes in Linux Command Line](http://www.tecmint.com/wp-content/uploads/2015/07/php-command-line-usage.jpeg)
Linux命令行中运行PHP代码——第一部分
PHP的语法和C、Java以及带有一些PHP特性的Perl变成语言中的语法十分相似它眼下大约正被2.6亿个网站所使用当前最新的稳定版本是PHP版本5.6.10。
PHP是HTML的嵌入脚本它便于开发人员快速写出动态生成的页面。PHP主要用于服务器端而Javascript则用于客户端以通过HTTP生成动态网页然而当你知道可以在Linux终端中不需要网页浏览器来执行PHP时你或许会大为惊讶。
本文将阐述PHP脚本语言的命令行方面。
**1. 在安装完PHP和Apache2后我们需要安装PHP命令行解释器。**
# apt-get install php5-cli [Debian and alike System)
# yum install php-cli [CentOS and alike System)
接下来我们通常要做的是,在‘/var/www/html这是 Apache2 在大多数发行版中的工作目录)这个位置创建一个内容为 <?php phpinfo(); ?>‘,名为 infophp.php 的文件来测试(是否安装正确),执行以下命令即可。
# echo '<?php phpinfo(); ?>' > /var/www/html/infophp.php
然后将浏览器指向http://127.0.0.1/infophp.php 这将会在网络浏览器中打开该文件。
![Check PHP Info](http://www.tecmint.com/wp-content/uploads/2015/07/Check-PHP-Info.png)
检查PHP信息
不需要任何浏览器在Linux终端中也可以获得相同的结果。在Linux命令行中执行/var/www/html/infophp.php
# php -f /var/www/html/infophp.php
![Check PHP info from Commandline](http://www.tecmint.com/wp-content/uploads/2015/07/Check-PHP-info-from-Commandline.png)
从命令行检查PHP信息
由于输出结果太大,我们可以通过管道将上述输出结果输送给 less 命令,这样就可以一次输出一屏了,命令如下:
# php -f /var/www/html/infophp.php | less
![Check All PHP Info](http://www.tecmint.com/wp-content/uploads/2015/07/Check-All-PHP-Info.png)
检查所有PHP信息
这里,‘-f选项解析病执行命令后跟随的文件。
**2. 我们可以直接在Linux命令行使用`phpinfo()`这个十分有价值的调试工具而不需要从文件来调用,只需执行以下命令:**
# php -r 'phpinfo();'
![PHP Debugging Tool](http://www.tecmint.com/wp-content/uploads/2015/07/PHP-Debugging-Tool.png)
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交互模式。
![Enable PHP Interactive Mode](http://www.tecmint.com/wp-content/uploads/2015/07/Enable-PHP-interactive-mode1.png)
启用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
![Create PHP Functions](http://www.tecmint.com/wp-content/uploads/2015/07/Create-PHP-Functions.png)
创建PHP函数
你可以一直运行该函数直至退出交互模式ctrl+z。同时你也应该注意到了上面输出结果中返回的数据类型为 NULL。这个问题可以通过要求 php 交互 shell用 return 代替 echo 返回结果来修复。
只需要在上面的函数的中 echo 声明用 return 来替换
替换
php { echo $a + $b;
php { return $a + $b;
剩下的东西和原理仍然一样。
这里是一个样例,在该样例的输出结果中返回了正确的数据类型。
![PHP Functions](http://www.tecmint.com/wp-content/uploads/2015/07/PHP-Functions.png)
PHP函数
永远都记住用户定义的函数不会从一个shell会话保留到下一个shell会话因此一旦你退出交互shell它就会丢失了。
希望你喜欢此次会话。保持连线,你会获得更多此类文章。保持关注,保持健康。请在下面的评论中为我们提供有价值的反馈。点赞并分享,帮助我们扩散。
还请阅读: [12个Linux终端中有用的的PHP命令行用法——第二部分][1]
--------------------------------------------------------------------------------
via: http://www.tecmint.com/run-php-codes-from-linux-commandline/
作者:[vishek 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/execute-php-codes-functions-in-linux-commandline/