Why 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)