亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊 [轉(zhuǎn)]PHP也可以當(dāng)成Shell Script

[轉(zhuǎn)]PHP也可以當(dāng)成Shell Script

Jun 21, 2016 am 09:13 AM
nbsp php quot script

PHP 怎么這么紅
??最近 PHP(Personal Hypertext Preprocessor) 似乎已經(jīng)成了這一兩年來 Linux/Unix 上最廣為大家所使用的網(wǎng)頁處理語言﹐它的方便、強(qiáng)大功能與 OpenSource 的特性使得它正逐漸侵蝕到傳統(tǒng) CGI 甚至是 MicroSoft ASP(Active Server Page)的市場﹐幾乎各大網(wǎng)站征招人才莫不以會(huì) PHP 作為基本條件。
??PHP 確實(shí)有這個(gè)資格可以這么紅﹐原因有下面數(shù)點(diǎn) :
??PHP 是 OpenSource 軟件﹐完全免費(fèi)﹐可以自由散布﹐因此吸引了極多的人來使用﹐也因?yàn)槿绱拴o吸引到了商業(yè)公司為其發(fā)展更好的引擎與最佳化軟件(請參考 http://www.zend.com/)。
??PHP 本身非常簡單易懂﹐淺顯的指令語法﹐外加一些基本的對象導(dǎo)向處理能力﹐讓新手足以在最短時(shí)間內(nèi)學(xué)會(huì)。
??PHP 提供了相當(dāng)多的功能﹐包含了數(shù)學(xué)處理、字符串處理、網(wǎng)絡(luò)相關(guān)功能、各種數(shù)據(jù)庫的支持、影像處理功能、有為數(shù)眾多的發(fā)展者正為 PHP 發(fā)展各式各樣的新功能﹐擴(kuò)充性極佳。
??PHP 非常容易與 Apache 相結(jié)合﹐作為 Apache 的模塊來使用﹐設(shè)定安裝上相當(dāng)簡單﹐也因?yàn)?Apache 目前已經(jīng)占據(jù)了 Web Server 全球 60% 的市場﹐PHP 自然而然成為 Apache 最佳搭配。
??不過﹐這次要講的主題不是 PHP 在網(wǎng)頁設(shè)計(jì)上的應(yīng)用﹐而是 PHP 在 Shell Script 上的應(yīng)用﹐一般所知的 Shell Script 大約就是 tcsh、bash、perl 或是 python 這幾類語言﹐我所要談的就是將 PHP 當(dāng)成 Shell Script 來使用。
??PHP 執(zhí)行檔的安裝
??一般 PHP 作為網(wǎng)頁處理語言都是要編譯成 Apache 的模塊﹐這里當(dāng)然不么做﹐也因此編譯起來很簡單﹐只要以 root 的身分進(jìn)行如下動(dòng)作 :
??解開 php-3.0.xx.tar.gz
??cd php
??configure
??make
??編譯完之后﹐在 php 目錄下有一個(gè)可執(zhí)行檔﹐檔名為 php﹐將它 copy 到 /usr/local/bin 下即可。注意﹐如果檔案太大﹐可以使用 strip 指令將 php 的方式將不必要的信息去除﹐這樣檔案就會(huì)小得多了。
??第一個(gè)程序
??開始撰寫我們的第一個(gè) PHP Shell Script 程序﹐這個(gè)例子印出 "Hello world !" :
??#!/usr/local/bin/php -q
??
???echo "Hello, world !";
???>
??注意到 PHP 原本是應(yīng)用在網(wǎng)頁應(yīng)用的﹐因此它內(nèi)定會(huì)送出 HTML 的 HEADER﹐但是在此我們是要將 PHP 用作 Shell Script﹐"-q" 就是表示不要送出 HEADER 的意思﹐你可以試試看不加上 -q 的顯示結(jié)果。
??在這個(gè)例子中﹐/usr/local/bin/php 是表示要執(zhí)行 /usr/local/bin/ 下的 PHP﹐因?yàn)槲覀儎偛艑⑺b在該處。echo 指令將 "Hello, world !" 印出﹐其中的 "" 字符是換行字符。
??注意到在將這個(gè)程序存成檔案后﹐須將其 chmod 成為可執(zhí)行屬性(chmod +x 文件名)﹐然后才能執(zhí)行喔。
??進(jìn)階使用 I
??有時(shí)候我們需要在程序執(zhí)行時(shí)﹐送進(jìn)一些參數(shù)﹐比如說 ls 這個(gè)指令﹐后面可以加上 -l 參數(shù)﹐PHP Shell Script 一樣也有支持這樣的用法﹐有兩個(gè)特殊的變量 : $argc 記錄著后面送入?yún)?shù)的個(gè)數(shù)﹐$argv[] 數(shù)組參數(shù)存著的則是參數(shù)的內(nèi)容。比如說我現(xiàn)在要設(shè)計(jì)一個(gè)算兩個(gè)數(shù)字總和的程序 :
??#!/usr/local/bin/php -q
??
???$sum=0;
???$sum=$sum+$argv[1]+$argv[2];
???echo $sum;
???>
??假設(shè)將此程序命名為 sum.php3﹐則執(zhí)行 sum.php3 1 2 按下 enter 則會(huì)印出 3。
??如果要算出不特定個(gè)數(shù)的參數(shù)和﹐那么就得要用到 $argc 這個(gè)特殊變量了 :
??#!/usr/local/bin/php -q
??
???$sum=0;
???for ($t=1;$t???$sum=$sum+$argv[$t];
???echo $sum;
???>
??假設(shè)將此程序命名為 bigsum.php3﹐則執(zhí)行 bigsum.php3 1 2 3 4 5 按下 enter 則會(huì)印出 15﹐執(zhí)行 bigsum.php3 1 2 3 4 5 6 按下 enter 則會(huì)印出 21。
??有時(shí)候我們需要在程序執(zhí)行中輸入資料﹐但是 PHP 原本就是用于網(wǎng)頁設(shè)計(jì)﹐而網(wǎng)頁上的資料輸入自然都是用 FORM 的方式來輸入﹐所以這將 PHP 作為 Shell Script 時(shí)問題就來了﹐好在 PHP 有提供了開文件功能﹐而在 Linux/Uinx 之下﹐輸入(input)這件事原本就可以用開檔的方式來完成﹐我們要開啟的是 /dev/stdin 這個(gè)設(shè)備檔(stdin 是表示 standard input 的意思)﹐程序如下 :
??#!/usr/local/bin/php -q
??
???$fp=fopen("/dev/stdin","r");
???$inputstr=fgets($fp,100);
???fclose($fp);
???
???echo "\n----------------------\n";
???echo $inputstr;
???>
??其中的 fgets($fp,100) 是指從 $fp 這個(gè)檔案(也就是 "/dev/stdin")中讀取出 100 個(gè) byte 的資料﹐程序執(zhí)行到這行便會(huì)停下來等待我們的輸入﹐當(dāng)我們輸入完按下 enter 之后﹐程序就會(huì)將剛才我們輸入的資料給印出來了。
??進(jìn)階使用 II
??雖然已經(jīng)可以處理輸入﹐但是這樣的功能顯然還是太簡單﹐無法應(yīng)付更大的應(yīng)用﹐比如說我需要一個(gè)功能是將一串資料流(data stream)中的 HTML 給去除﹐這時(shí)便需要完整地處理輸出輸入轉(zhuǎn)向的能力﹐我們可以先設(shè)計(jì)程序如下 :
??#!/usr/local/bin/php -q
??
???$fp=fopen("/dev/stdin","r");
???
???while(!feof($fp)) {
???$c=fgetc($fp);
???$inputstr=$inputstr.$c;
???};
???
???fclose($fp);
???
???echo $inputstr;
???>
??假設(shè)將此程序命名為 filt.php3﹐如果你直接執(zhí)行這個(gè)程序﹐它會(huì)一直等待你輸入﹐直到你按下 Ctrl+D 后才會(huì)將你的輸入資料給印出﹐我們可以這么執(zhí)行它 :
??more filt.php3 | filt.php3
??這樣的做法是將 filt.php3 這個(gè)程序用 more 給秀出并轉(zhuǎn)向給 filt.php3 這個(gè)程序﹐filt.php3 會(huì)不斷接受資料(事實(shí)上就是 filt.php3 程序代碼本身)﹐最后將其印出。
??我們可以在其中加上過濾 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;
???>
??假設(shè)將此程序命名為 filt2.php3﹐如此一來便完成了過濾功能﹐不信請拿個(gè) HTML 檔來試試看 :
??more xxx.html | filt2.php3
??你便會(huì)看到刪除了 HTML TAG 的文件了。
??結(jié)論
??PHP 拿來當(dāng) Shell Script 事實(shí)上相當(dāng)?shù)睾糜茅o原因是 PHP 本身很好學(xué)﹐而且它又支持了各種數(shù)據(jù)庫﹐當(dāng)你已經(jīng)經(jīng)常拿 PHP 來設(shè)計(jì)你的網(wǎng)站之后﹐絕對不太喜歡再使用其它的 Shell Script 語言來處理其它必須非網(wǎng)頁的部份﹐這時(shí)候拿 PHP 來當(dāng)做 Shell Script 的好處就會(huì)顯現(xiàn)出來了﹐你可以以一貫的方式來發(fā)展整個(gè)系統(tǒng)﹐而不必一下要用 PHP一下又用 Perl/Python 或是 C。
??國內(nèi)的 PHP 風(fēng)氣已經(jīng)相當(dāng)興盛﹐本站 LinuxFab 便是完全以 PHP 搭配 MySQL 發(fā)展起來的﹐事實(shí)上 PHP 的妙用還有很多﹐有機(jī)會(huì)日后再介紹﹐如果讀者們需要 PHP 的相關(guān)信息﹐歡迎到本站上的 PHP 論壇 參與更多討論。
???
??出自:http://linuxfab.cx?



Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
How to use PHP to build social sharing functions PHP sharing interface integration practice How to use PHP to build social sharing functions PHP sharing interface integration practice Jul 25, 2025 pm 08:51 PM

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

PHP calls AI intelligent voice assistant PHP voice interaction system construction PHP calls AI intelligent voice assistant PHP voice interaction system construction Jul 25, 2025 pm 08:45 PM

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization Jul 25, 2025 pm 08:57 PM

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy Jul 25, 2025 pm 08:27 PM

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism Jul 25, 2025 pm 08:30 PM

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

How to use PHP to combine AI to generate image. PHP automatically generates art works How to use PHP to combine AI to generate image. PHP automatically generates art works Jul 25, 2025 pm 07:21 PM

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Jul 27, 2025 am 04:31 AM

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

PHP integrated AI speech recognition and translator PHP meeting record automatic generation solution PHP integrated AI speech recognition and translator PHP meeting record automatic generation solution Jul 25, 2025 pm 07:06 PM

Select the appropriate AI voice recognition service and integrate PHPSDK; 2. Use PHP to call ffmpeg to convert recordings into API-required formats (such as wav); 3. Upload files to cloud storage and call API asynchronous recognition; 4. Analyze JSON results and organize text using NLP technology; 5. Generate Word or Markdown documents to complete the automation of meeting records. The entire process needs to ensure data encryption, access control and compliance to ensure privacy and security.

See all articles