Home > Backend Development > PHP Tutorial > shell calls php script and passes parameters

shell calls php script and passes parameters

WBOY
Release: 2016-07-28 08:25:56
Original
1380 people have browsed it

The command line executes the php script with parameters and obtains the parameters

Published on 2011-07-15

Category: php

First, why do we run the php script under the command line?

Personally understood, there are two main reasons:

1. Using crontab to run PHP can decompress the server. Of course, there is a condition here, that is, the real-time requirements are not high. For example: the real-time requirements for friend updates in SNS are not high, but the amount of data is relatively large. If run regularly at this time, it will put a lot of pressure on the web server and database server.

2. We need to complete a certain thing regularly. For example: I want to delete a user's message a month ago. At this time, the written php script is executed in crontab. It only needs to be run once a day. Instead of manually executing the php program.

Second, execute php with parameters under the command line and obtain the parameters

One thing is very important, that is, executing php under the command line, does not use apache and other such things, there is no http protocol, all get and post are transmitted The parameters have no effect at all, and an error will be reported, as follows:

zhangying@ubuntu:~$ php test.php?aaa=bbb

Could not open input file: test.php?aaa=bbb

General In some cases, there is no need to pass parameters to the scheduled php script, but sometimes, it is necessary.

1, test.php test file, very simple, right?

View copy and print?

  1. print_r($argv);
  2. echo "n";
  3. echo $argc;
  4. "n"
  5. ; ?> ;
  6. 2, call
  7. from the command line to view copy and print?

zhangying@ubuntu:~$ php test.php aaa ccc bbbb

  1. Array (
  2. [0] => test.php
  3. //Parameter 0, the file itself
  4. [1] => aaa //Parameter 1
  5. [2] => ; ccc                                                                          
  6. 4 //The value of $argc , the total number of parameters
  7. This way of passing parameters, the root shell script is really like, zhangying@ubuntu:~$ sh c1.sh aaa bbb
  8. I am c1.sh passes two parameters aaa bbb, and the shell will get three parameters. $0 is the file itself, $1 is parameter 1, and $2 is parameter 2. The difference is that php gets it in the form of an array, but the shell does not.
  9. The above introduces how shell calls PHP scripts and passes parameters, including aspects. I hope it will be helpful to friends who are interested in PHP tutorials.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template