Why is PHP so red?
Recently, PHP (Personal hypertext preprocessor) seems to have become the most widely used web-processing language in the last two years, and its convenience, powerful features and opensource characteristics make it gradually erode to the traditional CGI is even the market for MicroSoft ASP (Active Server Page), where almost every major web site recruits PHP as a basic condition.
PHP does have the qualifications to be so red, for the following reasons:
PHP is opensource software, completely free and free to spread, so it attracts a lot of people to use it, and because of this, it attracts business companies to develop better engine and optimization software (refer to http://www.zend.com/).
PHP itself is very easy to understand, simple instruction syntax, plus some basic object-oriented processing capabilities, so that novice enough in the shortest possible time to learn.
PHP provides a wide range of functions, including mathematical processing, string processing, network-related functions, support for various databases, image processing capabilities, a large number of development for PHP to develop a variety of new features, scalability is excellent.
PHP is very easy to combine with Apache, as the Apache module to use, Setup is quite simple, but also because Apache currently occupies 60% of the Web Server global market,php naturally become the best collocation of Apache.
However, this time the theme is not PHP in web design applications, but PHP in the shell script application, generally known shell script is about TCSH, bash, perl or Python languages, I would like to talk about the PHP Used as a Shell Script.
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.
Sometimes we need to enter the data in the program execution, but PHP is used for web design, and the data input on the page is naturally used form to input, so this will be PHP as a Shell Script when the problem comes, fortunately PHP has provided the ability to open files, and in the linux/u INX, enter (input) this matter can be used to open the way to complete, we want to open is/dev/stdin this device file (stdin is the meaning of 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;
?>
One of the fgets ($FP, 100) refers to from the $fp this file (that is, "/dev/stdin") read out of 100 byte data, the program execution to this line will stop to wait for our input, when we enter the finished press ENTER, the program will be the data we have just entered To print out.
Advanced Use II
Although the input can already be processed, but this function is obviously too simple to cope with a larger application, for example, I need a function is to remove the HTML in a stream of data streams, and then the ability to completely handle the output input steering, we can first design the program as follows:
#!/usr/local/bin/php-q
?
$FP =fopen ("/dev/stdin", "R");
Echo $inputstr;
?>
Suppose you name this program filt.php3, if you execute the program directly, it will wait for you to enter it until you press Ctrl+d to print your input data so that we can execute it:
More Filt.php3 | Filt.php3
This approach is to filt.php3 this program with more to show and turn to filt.php3 this program,FILT.PHP3 will continue to receive information (in fact, the FILT.PHP3 program code itself), and finally print it out.
We can add the ability to filter HTML:
#!/usr/local/bin/php-q
?
$FP =fopen ("/dev/stdin", "R");
Echo $inputstr;
?>
Suppose this program is named filt2.php3, this completes the filtering function, please take an HTML file to try:
More Xxx.html | Filt2.php3
You'll see that you've deleted the HTML TAG file.
Conclusion
PHP as a shell script is actually quite useful, because PHP itself is very easy to learn, and it supports a variety of databases, when you have often used PHP to design your site, you definitely do not like to use other Shell script language to deal with other parts that must not page, The advantage of using PHP as a Shell Script is that you can develop the whole system in a consistent way without having to use PHP to use Perl/python or C.
The domestic PHP atmosphere has been quite flourishing, the site Linuxfab is completely PHP with MySQL developed, in fact, there are many of the magical use of PHP, have the opportunity to introduce later, if the reader needs PHP information, welcome to the site of the PHP forum to participate in more discussions. (Source: Viphot)
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.