- 12 Useful PHP Commandline Usage Every Linux User Must Know
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- How to Use and Execute PHP Codes in Linux Command Line – Part 1
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- php — Unix, Linux Command
- SYNOPSIS
- DESCRIPTION
- OPTIONS
- FILES
- EXAMPLES
- SEE ALSO
- AUTHORS
- VERSION INFORMATION
- COPYRIGHT
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 – 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.
and then set anything (say Hi Tecmint ::) as PHP interactive shell command prompt, simply as:
Enable PHP Interactive Shell
Also you can set current time as your command Line Prompt, simply as:
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,
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 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.
In-case of single possibility, use TAB once.
You can keep pressing TAB for options till values of option are satisfied. All the activities are logged to file
To check your PHP interactive shell activity log, you may run:
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:
or a more explaining example is:
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.
The above both examples will output:
Print Base Name in PHP
11. You may create a file (say test1.txt) using php interactive shell at your Desktop, simply as
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.
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).
Sort the numbers in the array.
Print numbers of the array in sorted order along with their order. The first one is [0].
Sort Arrays in PHP
14. Get the value of Pi in PHP Interactive Shell
15. Print the square root of a number say 32
16. Echo a random number from the range be 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.
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.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
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 – 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.
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 , simply by running the below command.
and then point your browser to http://127.0.0.1/infophp.php which opens this file in web browser.
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:
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:
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 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.
Press ‘exit‘ or ‘ctrl+c‘ to close PHP interactive mode.
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.
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:
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.
Create a function and name it addition. Also declare two variables $a and $b.
Use curly braces to define rules in between them for this function.
Define Rule(s). Here the rule say to add the two variables.
All rules defined. Enclose rules by closing curly braces.
Test function and add digits 4 and 3 simply as :
Sample Output
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.
Sample Output
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‘
and rest of the things and principles remain same.
Here is an Example, which returns appropriate data-type in the output.
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.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
php — Unix, Linux Command
SYNOPSIS
DESCRIPTION
You can parse and execute files by using parameter -f followed by the name of the file to be executed.
Using parameter -r you can directly execute PHP code simply as you would do inside a .php file when using the eval() function.
It is also possible to process the standard input line by line using either the parameter -R or -F. In this mode each separate input line causes the code specified by -R or the file specified by -F to be executed. You can access the input line by $argn. While processing the input lines $argi contains the number of the actual line being processed. Further more the parameters -B and -E can be used to execute code (see -r) before and after all input lines have been processed respectively. Notice that the input is read from STDIN and therefore reading from STDIN explicitly changes the next input line or skips input lines.
If none of -r -f -B -R -F or -E is present but a single parameter is given then this parameter is taken as the filename to parse and execute (same as with -f). If no parameter is present then the standard input is read and executed.
OPTIONS
Tag | Description |
---|---|
—interactive -a | Run PHP interactively. This lets you enter snippets of PHP code that directly get executed. When readline support is enabled you can edit the lines and also have history support. |
—bindpath address:port|port -b address:port|port | |
Bind Path for external FASTCGI Server mode (CGI only). | |
—no-chdir -C | Do not chdir to the scripts directory (CGI only). |
—no-header -q | Quiet-mode. Suppress HTTP header output (CGI only). |
—timing count -T count | Measure execution time of script repeated count times (CGI only). |
—php-ini path|file -c path|file | Look for php.ini file in the directory path or use the specified file |
—no-php-ini -n | No php.ini file will be used |
—define foo[=bar] -d foo[=bar] | |
Define INI entry foo with value bar | |
-e | Generate extended information for debugger/profiler |
—file file -f file | Parse and execute file |
—global name -g name | Make variable name global in script. |
—help -h | This help |
—hide-args -H | Hide script name (file) and parameters (args. ) from external tools. For example you may want to use this when a php script is started as a daemon and the command line contains sensitive data such as passwords. |
—info -i | PHP information and configuration |
—syntax-check -l | Syntax check only (lint) |
—modules -m | Show compiled in modules |
—run code -r code | Run PHP code without using script tags |
—process-begin code -B code | |
Run PHP code before processing input lines | |
—process-code code -R code | |
Run PHP code for every input line | |
—process-file file -F file | |
Parse and execute file for every input line | |
—process-end code -E code | Run PHP code after processing all input lines |
—syntax-highlight -s | Output HTML syntax highlighted source |
—version -v | Version number |
—stripped -w | Output source with stripped comments and whitespace |
—zend-extension file -z file | |
Load Zend extension file | |
args. | Arguments passed to script. Use — args when first argument starts with - or script is read from stdin |
—rfunction | name |
—rf | name Shows information about function name |
—rclass | name |
—rc | name Shows information about class name |
—rextension | name |
—re | name Shows information about extension name |
—rextinfo | name |
—ri | name Shows configuration for extension name |
—ini | Show configuration file names |
FILES
Tag | Description |
---|---|
php-cli.ini | The configuration file for the CLI version of PHP. |
php.ini | The standard configuration file will only be used when php-cli.ini cannot be found. |
EXAMPLES
Tag | Description |
---|---|
php -r echo «Hello World\n»; | |
This command simply writes the text «Hello World» to standard out. | |
php -r print_r(gd_info()); | |
This shows the configuration of your gd extension. You can use this to easily check which image formats you can use. If you have any dynamic modules you may want to use the same ini file that php uses when executed from your webserver. There are more extensions which have such a function. For dba use: php -r print_r(dba_handlers(1)); | |
php -R echo strip_tags($argn).»\n»; | |
This PHP command strips off the HTML tags line by line and outputs the result. To see how it works you can first look at the following PHP command ‘php -d html_errors=1 -i‘ which uses PHP to output HTML formatted configuration information. If you then combine those two ‘php . |php . ‘ youll see what happens. | |
php -E echo «Lines: $argi\n»; | |
Using this PHP command you can count the lines being input. | |
php -R @$l+=count(file($argn)); -E echo «Lines:$l\n»; | |
In this example PHP expects each input line being a file. It counts all lines of the files specified by each input line and shows the summarized result. You may combine this with tools like find and change the php scriptlet. | |
php -R echo «$argn\n»; fgets(STDIN); | |
Since you have access to STDIN from within -B -R -F and -E you can skip certain input lines with your code. But note that in such cases $argi only counts the lines being processed by php itself. Having read this you will guess what the above program does: skipping every second input line. |
SEE ALSO
AUTHORS
Additional work for the CLI sapi was done by Edin Kadribasic, Marcus Boerger and Johannes Schlueter.
A List of active developers can be found here:
And last but not least PHP was developed with the help of a huge amount of contributors all around the world.
VERSION INFORMATION
COPYRIGHT
This source file is subject to version 3.01 of the PHP license, that is bundled with this package in the file LICENSE, and is available through the world-wide-web at the following url:
Источник