PHP can also be used as a shell script

Source: Internet
Author: User
Tags chmod echo command header html header

Installation of PHP execution files

General PHP as a web processing language is to be compiled into Apache module, here of course not to do, and therefore compiled very simple, as long as the identity of the root of the following actions:

Untie php-3.0.xx.tar.gz.

CD PHP

Configure

Make

After compiling, there is an executable file in the PHP directory, the filename is php, copy it to/usr/local/bin. Note that if the file is too large, you can use the strip instruction to remove unnecessary information from the PHP method so that the file will be much smaller.

First Program

Start writing our first PHP Shell Script, this example prints "Hello world!":

  #!/usr/local/bin/php -q
   echo "Hello, world !";
   ?>

Note that PHP is originally applied to web applications, so it will send out HTML header, But here we are to use PHP as Shell script, "q" means not to send the HEADER meaning, you can try not to add-Q display results.

In this example,,/usr/local/bin/php is to execute the php, under/usr/local/bin/because we have just put it in that place. The echo command will "Hello, world!" printed out, where the "" character is a newline character.

Note that after you save this program as a file, you must chmod it into an executable property (chmod +x file name) before you can execute it.

Advanced Use I

Sometimes we need to send some parameters in the execution of the program, such as LS, followed by the-l parameter,php Shell Script also support the use of the same, there are two special variables: $ARGC record the number of subsequent feed parameters, $argv [] array parameters stored in the is the contents of the parameter. Let's say I'm going to design a program that calculates the sum of two numbers:

  #!/usr/local/bin/php -q
   $sum=0;
   $sum=$sum+$argv[1]+$argv[2];
   echo $sum;
   ?>

Assuming that this program is named sum.php3,, the SUM.PHP3 1 2 press ENTER will print 3.

If you want to figure out the parameters of a specific number and, then you need to use the $ARGC this special variable:

  #!/usr/local/bin/php -q
     $sum=0;
   for ($t=1;$t<=$argc;$t++)
   $sum=$sum+$argv[$t];
   echo $sum;
   ?>

If you name this program as bigsum.php3, then execute BIGSUM.PHP3 1 2 3 4 5 Press ENTER to print out the 15, execution bigsum.php3 1 2 3 4 5 + 6 Press ENTER will print 21.

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.