PHP can also be used as a ShellScript_PHP tutorial

Source: Internet
Author: User
Tags html header
PHP can also be used as a ShellScript. PHP is so red recently, PHP (PersonalHypertextPreprocessor) seems to have become the most widely used web page processing language on LinuxUnix in the past one or two years. how is PHP so red?
Recent PHP (Personal Hypertext Preprocessor) it seems that it has become the most widely used web page processing language in Linux/Unix over the past one or two years. its convenience, powerful features and OpenSource features make it gradually eroded to traditional CGI and even microSoft ASP (Active Server Page) in the market, almost all major websites do not use PHP as the basic condition for recruiting talents.
PHP does have this qualification so red, for the following reasons:
PHP is an OpenSource software that is completely free and can be freely distributed. Therefore, it attracts a lot of people to use it. In addition, it attracts commercial companies to develop better engine and optimization software for it (please reference http://www.zend.com /).
PHP itself is very easy to understand, simple command syntax, and some basic object oriented processing capabilities, so that new users can learn in the shortest time.
PHP provides many functions, including mathematical processing, string processing, network-related functions, various database support, image processing functions, and various types of PHP development for many developers. new features provide excellent scalability.
PHP is very easy to combine with Apache. it is used as an Apache module and is quite simple to set and install. it is also because Apache currently occupies 60% of the global market for Web Server, and PHP naturally becomes the best Apache. matching.
However, the subject of this talk is not the application of PHP in web design, but the application of PHP in Shell Script. Generally, the Shell Script is about tcsh, bash, perl, or python. I want to talk about using PHP as a Shell Script.
Install PHP execution files
Generally, PHP, as a web processing language, must be compiled into an Apache Module. of course, this is not the case here. it is also very easy to compile, as long as the following actions are performed as root:
Unlock php-3.0.xx.tar.gz
Cd php
Configure
Make
After compilation, there is an executable file under the php directory named php, copy it to/usr/local/bin. Note: If the file size is too large, you can use the strip command to remove unnecessary information from php, so that the file size will be much smaller.
First program
Start to write our first PHP Shell Script program. This example shows "Hello world! ":
#! /Usr/local/bin/php-q
Echo "Hello, world! ";
?>
Note that PHP is originally applied on a webpage, so it will certainly send the html header, but here we want to use PHP as a Shell Script, "-q" means not to send the HEADER if-q is not added, try again.
In this example,/usr/local/bin/php indicates to execute PHP under/usr/local/bin/, because we just installed it here. The echo command will be "Hello, world! "Printed, where" "is a line break character.
Note that after saving the program as a file, you must convert its chmod into an executable attribute (chmod + x file name) before executing it.
Advanced use I
Sometimes we need to input some parameters during program execution, such as the ls command. the-l parameter can be added later. PHP Shell Script also supports this usage and has two special variables.: $ argc records the number of input parameters. $ argv [] array parameters are stored in the parameter content. For example, I want to design a program to calculate the sum of two numbers:
#! /Usr/local/bin/php-q
$ Sum = 0;
$ Sum = $ sum + $ argv [1] + $ argv [2];
Echo $ sum;
?>
Assume that the program is named sum. php3. then, execute sum. php3 1 2 and press enter to print 3.
If you want to calculate the parameter sum of an unspecified number, you need to use the special variable $ argc:
#! /Usr/local/bin/php-q
$ Sum = 0;
For ($ t = 1; $ t <= $ argc; $ t ++)
$ Sum = $ sum + $ argv [$ t];
Echo $ sum;
?>
Assume that the program is named bigsum. php3, bigsum is executed. php3 1 2 3 4 5 press enter to print 15 and execute bigsum. php3 1 2 3 4 5 6 press enter to print 21.
Sometimes we need to input data in the program execution, but PHP was originally used for webpage design, and the data input on the webpage is naturally input using FORM, so PHP is used as Shell Script the problem arises. Fortunately, PHP provides the file opening function, while under Linux/Uinx, input (input) you can use the archive method to enable/dev/stdin (stdin indicates standard input). The procedure is as follows:
#! /Usr/local/bin/php-q
$ Fp = fopen ("/dev/stdin", "r ");
$ Inputstr = fgets ($ fp, 100 );
Fclose ($ fp );

Echo "\ n ---------------------- \ n ";
Echo $ inputstr;
?>
Fgets ($ fp, 100) indicates the file from $ fp (that is, "/dev/stdin ") read 100 bytes of data, and the program will stop waiting for our input. after we press enter, the program will print the information we just entered yes.
Advanced application II
Although the input can be processed, such a function is obviously too simple to cope with larger applications. for example, I need a function to process a series of data streams) in this case, we need to completely process the output input and output redirection capabilities. we can first design the program as follows:
#! /Usr/local/bin/php-q
$ Fp = fopen ("/dev/stdin", "r ");

While (! Feof ($ fp )){
$ C = fgetc ($ fp );
$ Inputstr = $ inputstr. $ c;
};

Fclose ($ fp );

Echo $ inputstr;
?>
Assume that this program is named filt. php3: If you directly execute this program, it will wait for you to enter it until you press Ctrl + D to print your input data. we can execute it like this:
More filt. php3 | filt. php3
In this way, the filt. the php3 program uses more to show and turn to filt. php3, filt. php3 will continue to accept materials (in fact, it is filt. php3 program code itself), and finally print it out.
We can add the HTML filter function in it:
#! /Usr/local/bin/php-q
$ Fp = fopen ("/dev/stdin", "r ");

While (! Feof ($ fp )){
$ C = fgetc ($ fp );
$ Inputstr = $ inputstr. $ c;
};

Fclose ($ fp );

$ Inputstr = ereg_replace ("<([^ <>] *)>", "", $ inputstr );

Echo $ inputstr;
?>
Assume that the program is named filt2.php3, so the filtering function is completed. if you do not believe this, please try it with an HTML file:
More xxx.html | filt2.php3
You will see the file that deleted the html tag.
Conclusion
PHP is actually quite useful when it comes to Shell scripts, because PHP itself is very studious and supports a variety of databases. when you have used PHP to design your website, it is definitely not very good. like to use other Shell Script languages to process other parts that must not be webpages, the benefits of using PHP as a Shell Script will be displayed, and you can develop it in a consistent way. the entire system, instead of using PHP, Perl, Python, or C.
The domestic PHP culture has been quite prosperous, and the LinuxFab on this site is fully developed with PHP and MySQL. In fact, there are still a lot of wonderful PHP usage and there is a chance to introduce it later. if readers need PHP-related information welcome to the PHP forum on this site for more discussions. (Source: Viphot)

Http://www.bkjia.com/PHPjc/314161.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/314161.htmlTechArticlePHP how so red recently PHP (Personal Hypertext Preprocessor) seems to have become the most widely used web page processing language on Linux/Unix in the past one or two years, it's convenient, powerful...

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.