PHP can also become a shell script_php tutorial

Source: Internet
Author: User
Tags echo command html header
Why is PHP so red?
Recently PHP (Personal hypertext preprocessor) seems to have become the most widely used web-processing language in the past two years Linux/unix, its convenience, powerful features and opensource features make it gradually eroded to the traditional CGI is even the market for MicroSoft ASP (Active Server Page), with almost every major website recruiting people to use PHP as a basic condition.
PHP does have this qualification can be so red, for the following points:
PHP is opensource software, completely free and freely dispersed, so it attracts a lot of people to use it, and because of this, it attracts commercial companies to develop better engines and optimization software (see http://www.zend.com/).
PHP itself is very easy to understand, simple instruction syntax, plus some basic object-oriented processing ability, so that novice enough to learn in the shortest time.
PHP provides a considerable amount of functionality, including math processing, string processing, network-related functions, various database support, image processing capabilities, a large number of the development of PHP is developing a variety of new features, extensibility is excellent.
PHP is very easy to combine with Apache, as the Apache module to use, set up a very simple installation, also because Apache has now occupied the Web Server Global 60% Market,php naturally become the best combination of Apache.
However, this time to talk about the topic is not PHP in the application of Web design, but PHP in Shell script application, generally known shell script is about TCSH, bash, perl or Python these languages, all I want to talk about is the PHP Used as Shell Script.
Installation of PHP execution files
In 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 root of the following actions:
Untie php-3.0.xx.tar.gz
CD PHP
Configure
Make
After compiling, in the PHP directory there is an executable file named 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 in PHP's way, so that the file is much smaller.
First Program
Start writing our first PHP Shell Script program, this example prints "Hello world!":
#!/usr/local/bin/php-q
echo "Hello, world!";
?>
Notice that PHP was originally applied to the Web application, so it will send out the 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 means to execute the php, under/usr/local/bin/because we just loaded it there. The echo command prints "Hello, world!", where the "" character is a newline character.
Note that after you save this program as a file, you must chmod it as an executable property (chmod +x filename) before you can execute it.
Advanced Use I
Sometimes we need to send some parameters in the execution of the program, such as LS, which can be appended with the-l parameter,php Shell Script, there are two special variables: $ARGC records the number of arguments that are fed back, $ARGV [] The array parameter is stored is the contents of the parameter. For example, 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 the program is named sum.php3, then execute SUM.PHP3 1 2 press ENTER to print 3.
If you want to figure out a specific number of arguments, then you need to use $ARGC this particular variable:
#!/usr/local/bin/php-q
$sum = 0;
for ($t =1; $t <= $argc; $t + +)
$sum = $sum + $argv [$t];
Echo $sum;
?>
Assuming this program is named bigsum.php3, then execute BIGSUM.PHP3 1 2 3 4 5 Press ENTER to print out 15, execution bigsum.php3 1 2 3 4 5 6 Press ENTER to print 21.
Sometimes we need to enter data in the program execution, but PHP is originally used for web design, and the data input on the Web page is naturally used as a form to input, so this will php as a Shell Script when the problem comes, fortunately PHP has provided open file function, and in linux/u INX, input this thing can be done in the way of the open file, we want to open the/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 the $fp this file (that is, "/dev/stdin") read out 100 byte of the data, the program execution to this line will stop to wait for our input, when we enter the end of the press ENTER, the program will be the data we have just entered Printed out.
Advanced Use II
Although the input can already be processed, it is obviously too simple to handle a larger application, for example, I need a function to remove the HTML from a stream of data streams, which requires the ability to fully process the output input steering, and we can 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;
?>
Suppose this program is named filt.php3, if you execute the program directly, it will wait for you to enter it until you press Ctrl+d to print out your input data, and we can do it like this:
More Filt.php3 | Filt.php3
This is the practice of FILT.PHP3 this program with more to show and turn to filt.php3 this program,FILT.PHP3 will continue to receive information (in fact, FILT.PHP3 program code itself), and finally printed it out.
We can add the ability to filter 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;
?>
Assuming that this program is named filt2.php3, so that the filtering function is completed, do not believe that please take an HTML file to try:
More Xxx.html | Filt2.php3
You'll see the files that have the HTML TAG removed.
Conclusion
PHP is actually quite handy when it comes to shell scripts, because PHP itself is very studious, and it supports a variety of databases, and when you've been using PHP to design your site, you're definitely not very keen to use other shell script languages to handle other parts of the page that must be non-web. The benefit of using PHP as a Shell Script is obvious, and you can develop the whole system in a consistent way, without having to use PHP and Perl/python or C.
The domestic PHP culture has been quite prosperous, this site Linuxfab is completely PHP with MySQL developed, in fact, there are many magical use of PHP, there is a chance to introduce later, if the reader needs PHP information, welcome to the site of the PHP forum to participate in more discussion. (Source: Viphot)

http://www.bkjia.com/PHPjc/314161.html www.bkjia.com true http://www.bkjia.com/PHPjc/314161.html techarticle php How so Red recently PHP (Personal hypertext preprocessor) seems to have become the most widely used in the last two years Linux/unix the Web processing language, it's convenient, strong merit ...

  • 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.