Use PHP as the Shell scripting language

Source: Internet
Author: User
Many people may have thought about using PHP to write some programs such as timed sending, but they have no way to implement PHP on a regular basis. This article was invented once when I went to PHPBuilder, so I want to translate it for you (and make some corrections at the same time). I hope many people may have thought about using PHP to write some programs such as regular sending, but they have no measures to implement PHP on a regular basis; I invented this article when I went to PHPBuilder, so I wanted to translate it for you (and made some corrections at the same time). I hope it will be useful to you. Sorry for the first time I translated the article.
------------------------------------
We all know that PHP is a very good dynamic web page development language (fast speed, short development cycle ......). However, only a few people realize that PHP can also be a good language for writing Shell scripts. When PHP is used as a language for writing Shell scripts, it is not as powerful as Perl or Bash, but he has a good advantage, especially for people who are familiar with PHP but are not familiar with Perl.
To use PHP as the Shell scripting language, you must compile PHP as a binary CGI instead of the Apache mode. PHP compiled to run in binary CGI mode has some security titles, for the solution, see The PHP Manual (http://www.php.net ).
At the beginning, you may feel uncomfortable with writing Shell scripts, but it will be better: the only difference between using PHP as a general dynamic web page compiling language and using Shell scripting language is that a Shell script needs to describe the script program path in the first line of life:
#! /Usr/local/bin/php-q
We added the parameter "-1" after the PHP fulfillment file, so that PHP will not output HTTPHeader (if it still needs to be used as a dynamic Web page of the Web, then you need to apply the header function to output HTTPHeader ). Of course, in the Shell script, you still need to apply the PHP start and end labels:

Now let's look at an example to better understand the application of PHP as the Shell scripting language:
#! /Usr/local/bin/php-q
Print ('Hello, world! \ N ');
?>
The above program will briefly output "Hello, world !" To the Monitor.

1. pass the Shell script running parameters to PHP:
As a Shell script, some parameters are often used when running the program. as a Shell script, PHP has an embedded array "$ argv ", using the "$ argv" array, you can easily read the parameters during Shell script running ("$ argv [1]" corresponds to the first parameter, "$ argv [2]" corresponds to the second parameter, and so on ). For example, the following program:
#! /Usr/local/bin/php-q
$ First_name = $ argv [1];
$ Last_name = $ argv [2];
Printf ('Hello, % s! How are you today? \ N', $ first_name, $ last_name );
?>
The above code requires two parameters for running, namely, the first name and the first name. for example:
[Dbrogdon @ artemis dbrogdon] $ scriptname. ph Darrell Brogdon
The Shell script will output on the display:
Hello, Darrell Brogdon! How are you today?
[Dbrogdon @ artemis dbrogdon] $
When PHP is used as a dynamic web page programming language, it also contains the array "$ argv", but there are some differences with this: when PHP is used as the Shell scripting language, "$ argv [0]" corresponds to the script file name. when it is used for dynamic web page writing, "$ argv [1]" corresponds to the first parameter of QueryString.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.