Using and executing PHP code on the Linux command line

Source: Internet
Author: User
Tags echo date php debugging tools phpinfo sha1 square root

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.

    1. # apt-get install php5-cli [Debian 及类似系统]
    2. # 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.

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

The browser then accesses, 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:

    1. # 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:

    1. # 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:

    1. # 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.

  1. # php -a
  2. Interactive shell
  3. php > echo 2+3;
  4. 5
  5. php > echo 9-6;
  6. 3
  7. php > echo 5*4;
  8. 20
  9. php > echo 12/3;
  10. 4
  11. php > echo 12/5;
  12. 2.4
  13. php > echo 2+3-1;
  14. 4
  15. php > echo 2+3-1*3;
  16. 2
  17. 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.

    1. # 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:

    1. # chmod 755 phpscript.php

Then run it,

    1. # ./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.

    1. # php -a

Create a function to name itaddition. At the same time, declare two variables$aand a$b.

    1. php >function addition ($a, $b)

Use curly braces to define a rule for the function in the meantime.

    1. php >{

Define the rule. Here, this rule is about adding these two variables.

    1. php { echo $a + $b;

All rule definitions are complete and the rules are encapsulated by closing curly braces.

    1. php {}

To test the function, add the numbers 4 and 3 with the following command:

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

Sample output
    1. 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.

    1. php > var_dump (addition(a,b));
    1. php > var_dump (addition(9,3.3));

Sample output
    1. 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.


    1. php { echo $a + $b;


    1. 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.

    1. $ php -a

Then, set anything (Say hi Tecmint::) as a command prompt for the PHP interactive shell, as follows:

    1. 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:

    1. php >#cli.prompt=`echo date(‘H:m:s‘);` >
    2. 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:

    1. $ php -a
    2. 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.

    1. 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.

    1. php > ZIP [TAB][TAB]

If there is only one possibility, just use the TAB key once.

    1. 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:

    1. $ 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:

    1. php > echo "color_code1 TEXT second_color_code";

Specifically, it is:

    1. 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 # # #.

    1. php > echo basename("/var/www/html/wp/wp-content/plugins");
    2. php > echo basename("");

The two examples above will output:

    1. plugins
    2. 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

    1. 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

The Strlen function is used to get the length of the specified string.

    1. php > echo strlen("");

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).

    1. php > $a=array(7,9,2,5,10);

Sorts the numbers in an array.

    1. php > sort($a);

Prints the numbers in the array in sorted order, printing the ordinal number, and the first one is [0].

  1. php > print_r($a);
  2. Array
  3. (
  4. [0]=>2
  5. [1]=>5
  6. [2]=>7
  7. [3]=>9
  8. [4]=>10
  9. )

Sort an array in PHP

14. Get the value of π in the PHP interactive shell

    1. php > echo pi();
    2. 3.1415926535898

15. Print a number such as the square root of 32

    1. php > echo sqrt(150);
    2. 12.247448713916

16. Select a random number from the 0-10 range

    1. 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.

    1. php > echo md5(avi);
    2. 3fca379b3f0e322b7b7967bfcfb948ad
    3. php > echo sha1(avi);
    4. 8f920f22884d6fea9df883843c4a8095a2e5ac6f
    1. $ echo -n avi | md5sum
    2. 3fca379b3f0e322b7b7967bfcfb948ad-
    3. $ echo -n avi | sha1sum
    4. 8f920f22884d6fea9df883843c4a8095a2e5ac6f-

Check the MD5 checksum SHA1 checksum in PHP

Using and executing PHP code on the Linux command line

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.