Why is PHP so popular
Recently, PHP (Personal Hypertext Preprocessor) seems to have become the most widely used web page processing language on Linux/Unix in the past two years. Its convenience, powerful functions and OpenSource features make it a popular choice. Gradually eroding the market of traditional CGI and even MicroSoft ASP (Active Server Page), almost all major websites recruit talents with the basic requirement of knowing PHP.
PHP does have this qualification to be so popular. The reasons are as follows:
PHP is OpenSource software, completely free and can be freely distributed. Therefore, it has attracted a lot of people to use it. Because of this, it has attracted business The company develops better engines and optimization software for it (please refer to http://www.zend.com/).
PHP itself is very simple and easy to understand, with simple command syntax and some basic object-oriented processing capabilities, allowing novices to learn it in the shortest time.
PHP provides quite a lot of functions, including mathematical processing, string processing, network-related functions, various database support, image processing functions, and a large number of developers are developing various new functions for PHP. Functional and extensible.
PHP is very easy to combine with Apache. It can be used as a module of Apache. The setting and installation are quite simple. And because Apache currently accounts for 60% of the global Web Server market, PHP naturally becomes the best match for Apache.
However, the topic I want to talk about this time is not the application of PHP in web design, but the application of PHP in Shell Script. The commonly known Shell Script is about tcsh, bash, perl or python. What I'm talking about is using PHP as a Shell Script.
Installation of PHP executable file
Generally, PHP as a web page processing language needs to be compiled into an Apache module. Of course, this is not done here. Therefore, compilation is very simple. Just perform the following actions as root:
Unlock php-3.0.xx.tar.gz
cd php
configure
make
After compilation, there will be an executable file in the php directory, the file name is php, copy it Just go to /usr/local/bin. Note that if the file is too large, you can use the strip command to remove unnecessary information in PHP, so that the file will be much smaller.
The first program
Start writing our first PHP Shell Script program. This example prints "Hello world!":
#!/usr/local/bin/php -q
echo "Hello, world!";
?>
Note that PHP was originally used in web applications, so it will send the HTML HEADER by default, but here we are going to PHP is used as Shell Script. "-q" means not to send HEADER. You can try it without adding -q to display the result.
In this example, /usr/local/bin/php means to execute PHP under /usr/local/bin/ because we just installed it there. The echo command prints "Hello, world!", where the "" character is a newline character.
Note that after saving this program as a file, you must chmod it to become executable (chmod +x file name) before it can be executed.
Advanced Usage I
Sometimes we need to enter some parameters when the program is executed, such as the ls command, which can be followed by the -l parameter. PHP Shell Script also supports this usage. There are two A special variable: $argc records the number of parameters sent later, and the $argv[] array parameter stores the content of the parameters. 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 this program is named sum.php3, then execute sum.php3 1 2 Press enter 3 will be printed.
If you want to calculate the sum of an unspecified number of parameters, you have 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;
?>
Suppose this program is named bigsum.php3, then execute bigsum.php3 1 2 3 4 5 and press enter, and 15 will be printed. Execute bigsum.php3 1 2 3 4 5 6 and press enter. will print 21.
Sometimes we need to input data during program execution, but PHP was originally used for web design, and the data input on the web page is naturally entered using FORM, so this is a problem when using PHP as a Shell Script. Fortunately, PHP provides a file opening function. Under Linux/Uinx, input can be done by opening a file. What we want to open is the device file /dev/stdin. (stdin means standard input), the program is as follows:
#!/usr/local/bin/php -q
$fp=fopen("/dev/stdin"," r");
$inputstr=fgets($fp,100);
fclose($fp);
echo "n------------- --------n";
echo $inputstr;
?>
where fgets($fp,100) refers to the file $fp (that is, "/dev/ stdin"), the program will stop and wait for our input when it reaches this line. When we finish inputting and press enter, the program will print out the data we just entered.
Advanced Use II
Although it can already process input, this function is obviously still too simple and cannot cope with larger applications. For example, I need a function to process input from a data stream. HTML is removed, then we need the ability to completely handle input and output steering. 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 this program is named filt.php3 If you execute this program directly, it will wait for your input until you press Ctrl+D before printing out your input data. We can execute it like this:
more filt.php3 | filt. php3
This approach is to use more to show the filt.php3 program and redirect it to the filt.php3 program. filt.php3 will continue to receive data (in fact, it is the filt.php3 program code itself), and finally it will Print out.
We can add the function of filtering HTML:
#!/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 this program is named filt2.php3, in this way the filtering function is completed. If you don’t believe it, please try it with an HTML file:
more xxx .html | filt2.php3
You will see the file with the HTML TAG deleted.
Conclusion
PHP is actually quite easy to use as Shell Script. The reason is that PHP itself is easy to learn and it supports various databases. When you have often used PHP to design your website, it is absolutely I don't like to use other Shell Script languages to deal with other parts that must not be web pages. At this time, the advantages of using PHP as Shell Script will appear. You can develop the entire system in a consistent way without having to If you want to use PHP, use Perl/Python or C.
Domestic PHP trend has become quite prosperous. This site, LinuxFab, is completely developed with PHP and MySQL. In fact, there are many wonderful uses of PHP. I will introduce it in the future if I have the opportunity. If readers need PHP-related information, welcome Join the discussion in the PHP forum on this site. (Source: Viphot)