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.
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 ::
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
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();
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
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";
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
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");
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); Array ( [0] => 2 [1] => 5 [2] => 7 [3] => 9 [4] => 10 )
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);
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); 3fca379b3f0e322b7b7967bfcfb948ad php > echo sha1(avi); 8f920f22884d6fea9df883843c4a8095a2e5ac6f
$ echo -n avi | md5sum 3fca379b3f0e322b7b7967bfcfb948ad - $ echo -n avi | sha1sum 8f920f22884d6fea9df883843c4a8095a2e5ac6f -
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.
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.
Sometimes i used php -r to execute some simple php code for example simple php calculator php -r ‘echo 2**8+10;’
Thanks for the feedback @Yoander,
We have already mentioned it in the first part of this post. You may find it here: https://www.tecmint.com/run-php-codes-from-linux-commandline/