Summary of precautions for executing PHP scripts on the PHP command line

Source: Internet
Author: User
Tags php code php script stdin

If you use the wamp integrated installation environment, your php configuration is at D:/wamp/bin/apache/Apache2.2.17/bin.
You need to copy and overwrite php under D:/wamp/bin/php/php5.3.3. ini. Otherwise, an error such as Fatal error: Call to undefined function will be reported when you Call the extended function.

If you are too lazy to write such a long string of php paths, you can also add D:/wamp/bin/php/php5.3.3 to the environment variable path.
In addition, we have questions about parameter passing. For example, I want to execute test. php? A = 123
In the command line, we can write php test. php 123
Use $ argv [1] in test. php to receive 123.

Create a simple text file that contains the following PHP code and save it as hello. php:

The code is as follows: Copy code

<? Php
Echo "Hello from the CLI ";
?>

Now, try to run this program at the command line prompt by calling the CLI executable file and providing the Script File name:

The code is as follows: Copy code
# Php phphello. php
Output Hello from the CLI

Attach a bat executable file for reference

The code is as follows: Copy code

@ Echo off

Php D:/wamp/www/taobao/items. php 158345687

Php D:/wamp/www/taobao/refunds_up.php 158345687

Php D:/wamp/www/taobao/trade. php 158345687

Echo. & echo press any key to close the BAT window... & pause

Exit


Code for executing common commands

The following are the command line mode option parameters provided by the PHP binary file (php.exe program). You can use the PHP-h command to query these parameters at any time.
Usage: php [options] [-f] <file> [args...]
Php [options]-r <code> [args...]
Php [options] [-- args...]
-S Display color syntax highlighted source.
-W Display source with stripped comments and whitespace.
-F <file> Parse <file>.
-V Version number
-C <path> | <file> Look for php. ini file in this directory
-A Run interactively
-D foo [= bar] Define INI entry foo with value 'bar'
-E Generate extended information for debugger/profiler
-Z <file> Load Zend extension <file>.
-L Syntax check only (lint)
-M Show compiled in modules
-I PHP information
-R <code> Run PHP <code> without using script tags <?...>
-H This help
 
Args... Arguments passed to script. Use -- args when first argument
Starts with-or script is read from stdin

The cli sapi module has the following three methods to obtain the PHP code you want to run:

In windows, use double quotation marks as much as possible. In linux, use single quotation marks as much as possible.


1. Run the specified file in PHP.

The code is as follows: Copy code

Php my_script.php
 
Php-f "my_script.php"

The above two methods (using or without the-f parameter) can run the given my_script.php file. You can select any file to run the script. The PHP script you specified does not have to use. php as the extension. They can have any file name and extension.

2. Run the PHP code directly on the command line.

The code is as follows: Copy code

Php-r "print_r (get_defined_constants ());"

When using this method, pay attention to the substitution of shell variables and the use of quotation marks.

Note: read the preceding example carefully. There is no start or end mark when running the code! After the-r parameter is added, these tokens are not required, and adding them will lead to syntax errors.

3. Provide the PHP code to be run through standard input (stdin.

The above usage provides us with very powerful functions, so that we can dynamically generate PHP code and run the code through the command line as follows:

The code is as follows: Copy code

$ Some_application | some_filter | php | sort-u> final_output.txt

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.