20150722-2 选题

This commit is contained in:
DeadFire 2015-07-22 15:23:51 +08:00
parent 32a138bddc
commit caee8c0f63
2 changed files with 386 additions and 0 deletions

View File

@ -0,0 +1,203 @@
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,183 @@
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/