search
HomeBackend DevelopmentPHP TutorialShellScript method of PHP conversion This method is quite similar to PERL's CGI method. . _PHP Tutorial

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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.