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”, 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
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
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
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
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
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 "3[0;31m Hi Tecmint \x1B[0m";
Enable Colors in PHP Shell
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:

Print Base Name in PHP
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
Print Length String in PHP
13. PHP Interactive shell can sort an array. Yes you heard it right

Declare Variable a and set it’s 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);
    [0] => 2
    [1] => 5
    [2] => 7
    [3] => 9
    [4] => 10
Sort Arrays in PHP
Sort Arrays in PHP
14. Get the value of Pi in PHP Interactive Shell
php > echo pi();

15. Print the square root of a number say 32
php > echo sqrt(150);

16. Echo a random number from the range be 0-10
php > echo rand(0, 10);
Get Random Number in PHP
Get Random Number in PHP
17. Get md5sum and sha1sum for a given string
For example, let’s 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);

php > echo sha1(avi);
$ echo -n avi | md5sum
3fca379b3f0e322b7b7967bfcfb948ad  -

$ echo -n avi | sha1sum
8f920f22884d6fea9df883843c4a8095a2e5ac6f  -
Check md5sum and sha1sum in PHP
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. That’s 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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

A Passionate GNU/Linux Enthusiast and Software Developer with over a decade in the field of Linux and Open Source technologies.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.


Leave a Reply
  1. Hello,

    I have an application that runs on localhost fine starting from terminal ex: ‘root@debian:/usr/local/lib/file# php -S localhost:8000’. I’m trying to get it to execute on boot and have no idea how to format for startup, it won’t execute in pm2, nor know how to add properly formated to rc.local, etc. I’ve looked for hours on end around net to no avail. Writing scripts is quite beyond my skill-set at this point. Most advice seems to center around either a complicated script or adding a simple command to cron, init.d or rc.local. I’m a cut and paste guy and usually need highly relevant concrete examples per instance. Any ideas?

    • Dear MaQ,
      As I am able to understand, you have an application that you want to start at boot automatically. You may run as root
      # update-rc.d Application_name defaults [Replace Application_name with the name of Application]

      To start something after reboot, open file /etc/crontab as root and enter the below line. Save and exit.

      @reboot /path/to/script

      Don’t forget to replace /path/to/script with original path.

      If you are unable to fix the issue yourself, you may forward me your SSH credentials with complete detail on OS and Application on my email id – avishek1210[at]gmail.com, for better assistance.

  2. Sometimes i used php -r to execute some simple php code for example simple php calculator php -r ‘echo 2**8+10;’


Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.