PHP is an open source server-side scripting language, originally three letters representing the "Personal Home page", and now stands for "Php:hypertext preprocessor", which is a recursive acronym. It is a cross-platform scripting language that is heavily influenced by C, C + +, and Java.
Running PHP code on the Linux command line
PHP's syntax is very similar to the syntax of C, Java, and Perl with some PHP features, which is currently used by 260 million web sites, and the latest stable version is PHP version 5.6.10.
PHP is an embedded script for HTML that enables developers to quickly write dynamically generated pages. PHP is primarily used for server-side (while JavaScript is used by clients) to generate dynamic Web pages over HTTP, but you might be surprised when you know you can execute PHP without a Web browser in your Linux terminal.
This article explains the command-line aspects of the PHP scripting language.
1. After installing PHP and Apache2, we need to install the PHP command line interpreter.
- # apt-get install php5-cli [Debian 及类似系统]
- # yum install php-cli [CentOS 及类似系统]
The next thing we usually do is, in/var/www/htmlthis location (which is the working directory in most distributions), create a Apache2<?php phpinfo(); ?>infophp.phpfile to test (PHP is installed correctly) and execute the following command.
- # echo ‘<?php phpinfo(); ?>‘ > /var/www/html/infophp.php
The browser then accesses http://127.0.0.1/infophp.php, which opens the file in a Web browser.
Check PHP information
No browser is required and the same results can be obtained from the Linux terminal. Execute on the linux command line/var/www/html/infophp.php, such as:
- # php -f /var/www/html/infophp.php
Checking PHP information from the command line
Because the output is too large, we can pass the above output to the command through the pipeline,lessso we can output one screen at a time, the command is as follows:
- # php -f /var/www/html/infophp.php | less
Check all PHP information
Here, the '-f ' option resolves and executes the file followed by the command.
2. We can use this very valuable debugging tool directly on the Linux command line withoutphpinfo()having to call it from a file, just execute the following command:
- # php -r ‘phpinfo();‘
PHP Debugging Tools
Here, the '-r ' option will allow the PHP code to execute directly in the Linux terminal without<and>tagging.
3. Run PHP in interactive mode and do some math operations. Here, the '-a ' option is used to run 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
Enter ' exit ' or press ' CTRL + C ' to turn off PHP interactive mode.
Enable PHP interactive mode
4. You can just run the PHP script as shell scripts. First, create a PHP sample script in your current working directory.
- # echo -e ‘#!/usr/bin/php\n<?php phpinfo(); ?>‘ > phpscript.php
Notice that we used the first line of the PHP script#!/usr/bin/php, just like in the shell script (/bin/bash). The first line#!/usr/bin/phptells the Linux command line to parse the script file with the PHP interpreter.
Next, let the script execute:
- # chmod 755 phpscript.php
Then run it,
- # ./phpscript.php
5. You can create simple functions entirely on your own by interacting with the shell, which you are sure to be surprised by. The following is a step-up guide.
Turn on PHP interactive mode.
- # php -a
Create a function to name itaddition. At the same time, declare two variables$aand a$b.
- php >function addition ($a, $b)
Use curly braces to define a rule for the function in the meantime.
- php >{
Define the rule. Here, this rule is about adding these two variables.
- php { echo $a + $b;
All rule definitions are complete and the rules are encapsulated by closing curly braces.
- php {}
To test the function, add the numbers 4 and 3 with the following command:
- php > var_dump (addition(4,3));
Sample output
- 7NULL
You can run the following code to execute the function, you can test the different values, you want to think how many times the line. Replace the A and B inside to your own value.
- php > var_dump (addition(a,b));
- php > var_dump (addition(9,3.3));
Sample output
- 12.3NULL
Creating PHP Functions
You can run the function until you exit interactive mode (CTRL + Z). Also, you should notice that the data type returned in the output above is NULL. This problem can be done by requiring the PHP interactive shell to return??? echo returns the result to fix.
You only need to replace the ' echo ' declaration with ' return ' in the function above.
Replace
- php { echo $a + $b;
For
- php {return $a + $b;
The rest is still the same as the principle.
Here is a sample that returns the correct data type in the output of the example.
PHP functions
Always remember that user-defined functions are not kept from a shell session to the next shell session, so once you exit the interactive shell, it is lost.
6. Set the PHP command-line prompt
To set the PHP command prompt, you need to open a PHP interactive shell using the following php-a (enable PHP interactive mode) command in the Linux terminal.
- $ php -a
Then, set anything (Say hi Tecmint::) as a command prompt for the PHP interactive shell, as follows:
- php >#cli.prompt=Hi Tecmint ::
Enable PHP Interactive Shell
Also, you can set the current time as your command line prompt with the following actions:
- php >#cli.prompt=`echo date(‘H:m:s‘);` >
- 22:15:43>
7. Output one screen at a time
In our previous article, we have used commands in many places through pipelines in the original commandless. By doing this, we can get a split-screen display where all the output is not available on a single screen. However, we can configure the php.ini file by setting the value of pager to less to output one screen at a time, as follows:
- $ php -a
- php >#cli.pager=less
Limit PHP Screen output
This way, the next time you run a command (such as a debugger) and thephpinfo();output of the command is too large to be fixed on a screen, it will automatically produce the output that is appropriate for your current screen.
- php > phpinfo();
PHP Information output
8. Suggestions and TAB completion
The PHP shell is smart enough to show you suggestions and tab completion, which you can use with the TAB key. If you have multiple options for a string that you want to use for tab completion, you need to use the TAB key two times to do it, and in other cases you can use it once.
If there is more than one possibility, use the TAB key two times.
- php > ZIP [TAB][TAB]
If there is only one possibility, just use the TAB key once.
- php >#cli.pager [TAB]
You can always press the TAB key to get the suggested completion until the value meets the requirements. All actions are logged to the~/.php-historyfile.
To check your PHP interactive shell activity log, you can perform:
- $ nano ~/.php_history | less
Check PHP interactive shell logs
9. You can use the color in the PHP interactive shell, all you need to know is the color code.
Use Echo to print the output of a variety of colors, similar to this:
- php > echo "color_code1 TEXT second_color_code";
Specifically, it is:
- php > echo "\033[0;31m Hi Tecmint \x1B[0m";
Enable color in PHP shell
So far, as we've seen, pressing the ENTER key means executing the command, but the semicolon at the end of each command in the PHP shell is required.
10. Use BaseName () in the PHP shell to output the last part of the path
The basename function in the PHP shell can be given from the last part that contains the path to the file or directory.
basename () Examples # # and # # #.
- php > echo basename("/var/www/html/wp/wp-content/plugins");
- php > echo basename("www.tecmint.com/contact-us.html");
The two examples above will output:
- plugins
- contact-us.html
Print the base name in PHP
11. You can use the PHP interactive shell to create files (such as Test1.txt) on your desktop, as simple as the following
- php> touch("/home/avi/Desktop/test1.txt");
We've seen how good the PHP interaction shell is in math, and there are a few more examples that will surprise you.
12. Use the PHP interactive shell to print the length of a string such as tecmint.com
The Strlen function is used to get the length of the specified string.
- php > echo strlen("tecmint.com");
Print the length of a string in PHP
PHP Interactive shell can be sorted by array, yes, you heard the wrong
Declare the variable A and set its value to array (7,9,2,5,10).
- php > $a=array(7,9,2,5,10);
Sorts the numbers in an array.
- php > sort($a);
Prints the numbers in the array in sorted order, printing the ordinal number, and the first one is [0].
-
- php > print_r($a);
-
- Array
-
- (
-
- [0]=>2
-
- [1]=>5
-
- [2]=>7
-
- [3]=>9
-
- [4]=>10
-
- )
Sort an array in PHP
14. Get the value of π in the PHP interactive shell
- php > echo pi();
- 3.1415926535898
15. Print a number such as the square root of 32
- php > echo sqrt(150);
- 12.247448713916
16. Select a random number from the 0-10 range
- php > echo rand(0,10);
Get random numbers in PHP
17. Get the MD5 checksum SHA1 checksum for a specified string, for example, let's check the MD5 checksum SHA1 checksum of a string (such as AVI) in the PHP shell and cross-check the results of the MD5 checksum SHA1 checksum generated by the bash shell.
- php > echo md5(avi);
- 3fca379b3f0e322b7b7967bfcfb948ad
- php > echo sha1(avi);
- 8f920f22884d6fea9df883843c4a8095a2e5ac6f
- $ echo -n avi | md5sum
- 3fca379b3f0e322b7b7967bfcfb948ad-
- $ echo -n avi | sha1sum
- 8f920f22884d6fea9df883843c4a8095a2e5ac6f-
Check the MD5 checksum SHA1 checksum in PHP
http://www.tecmint.com/run-php-codes-from-linux-commandline/
http://www.tecmint.com/execute-php-codes-functions-in-linux-commandline/
Using and executing PHP code on the Linux command line