This article mainly introduces the php command line usage and analyzes the methods for running php programs in the command line in the form of instances, which is of great practical value, for more information about php command line usage, see the following example. The usage analysis is as follows:
Php is a very popular web server scripting language. In fact, php can not only play an important role in web servers. It can be executed in the same way as the command line.
In this article, I will introduce how to use php in the command line.
1. View php version and configuration
Enter php-v in the command line to view the current php version.
The code is as follows:
PHP 5.2.17 (cli) (built: Feb 2 2011 11:19:21)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
With Zend Optimizer v3.3.9, Copyright (c) 1998-2009, by Zend Technologies
With eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator
Other options include-m and-I. I will not give a column here.
-M displays the valid modules loaded by php.
-I: No phpinfo in html format is output.
You can use the-ini option to output the quantity and path information of the ini configuration file loaded by the current php.
As shown in:
2. run the php program in the command line.
It is very easy to run php from the command line. However, you need to know some precautions. Server variables such as $ _ SESSION cannot be used in the command line, and other code runs exactly the same as pai_^ on the web server.
The code is as follows:
<? Php
Echo "run php command line echo ";
?>
Save the above code as hello. php. Input php-f hello. php in the command line. Shows the result:
One of the advantages of executing the php file in the command line is that you can execute some scheduled tasks through scripts. Instead of using the web server pai_^.
Of course, we can also directly debug the code in php: Enter the php-r command and a ">" symbol will appear. This indicates that you have entered the php shell and can directly write and execute the code.
The code is as follows:
-Bash-3.2 $ php-R'
> For ($ I = 0; $ I <2; $ I ++ ){
> Echo "Number: {$ I} \ n ";
>}
>'
Number: 0
Number: 1
You can also use the php-a command to open the interactive mode and enter a line of code. php will output the result in real time.
3. check php syntax and highlight output
You do not need to execute the code. we can check the php file syntax errors in the command line.
The code is as follows:
-Bash-3.2 $ php-l hello. php
No syntax errors detected in hello. php
Programmers often need to highlight the php code as is and use php-s.
The code is as follows:
-Bash-3.2 $ php-s hello. php
<?php
echo
'ddd'
;
style="color: #0000BB">?>
The display effect in the browser is as follows:4. view the php Manual
Starting from php5.1.2, programmers can view the manual under the php command line and enter the php-rf function. The syntax of the function is printed.
The code is as follows:
-Bash-3.2 $ php -- rf strip_tags
Function [ Function strip_tags] {
-Parameters [2] {
Parameter #0 [ $ Str]
Parameter #1 [ $ Allowable_tags]
}
}
If you want to view the class using-rc; view the extension using-re.
The usage of Php command line is now over. I hope this article will help you with PHP programming.