Home  >  Article  >  Backend Development  >  ShellScript method of PHP conversion This method is quite similar to PERL's CGI method. . _PHP Tutorial

ShellScript method of PHP conversion This method is quite similar to PERL's CGI method. . _PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:28:43754browse

Shell Script PHP 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 It is gradually eroding the market of traditional CGI and even MicroSoft ASP (Active Server Page). Almost all major websites recruit talents with PHP as a basic condition. PHP does have this qualification to become 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 commercial companies to develop it better. Engine and optimization software (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, support for various databases, and image processing functions. A large number of developers are developing various new functions and expansions for PHP. Sex is excellent. PHP is very easy to combine with Apache. It can be used as a module of Apache. The configuration 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 want to talk about What we are 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, so compilation is very simple. Just perform the following actions as root: Unzip 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. Just copy it 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 starts 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 using PHP as a Shell Script. "-q" means not to send the HEADER. You can try the display result without adding -q. 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!", in which the "" character is a newline character. Note that after saving this program into a file, you must chmod it to become executable (chmod +x file name) before it can be executed. Advanced use of I Sometimes we need to send some parameters when the program is executed. For example, the ls command can be followed by the -l parameter. PHP Shell Script also supports this usage. There are two special variables: $ 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 that calculates 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 Assume 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, and 21 will be printed. 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 problem arises when using PHP as a Shell Script. Okay PHP provides a file opening function, but under Linux/Uinx, input can be done by opening a file. What we want to open is the device file /dev/stdin (stdin is 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 " ---------------------- "; echo $inputstr; ?> where fgets($fp,100) refers to the file $fp (that is, "/dev/stdin" ) to read 100 bytes of data. When the program reaches this line, it will stop and wait for our input. When we finish inputting and press enter, the program will print out the data we just entered.Advanced Use II Although it can already handle input, this function is obviously still too simple and cannot cope with larger applications. For example, I need a function to remove HTML from a series of data streams. At this time We need the ability to completely handle output and input 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 that 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 method is to replace the filt.php3 program Use more to show and redirect to the filt.php3 program. filt.php3 will continue to receive data (actually the filt.php3 program code itself) and finally print it 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("

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531741.htmlTechArticleShell Script PHP Why is PHP so popular? Recently, PHP (Personal Hypertext Preprocessor) seems to have become the most popular one in the past two years. The most widely used web page processing language on Linux/Unix...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn