會(huì)自動(dòng)補(bǔ)充的字串截取函數(shù)_PHP
Jun 01, 2016 pm 12:40 PM
PHP的優(yōu)點(diǎn)之一是速度很快,對(duì)于一般的網(wǎng)站應(yīng)用,可以說(shuō)是已經(jīng)足夠了。不過(guò)如果站點(diǎn)的訪問(wèn)量很高、帶寬窄或者其它的因素令服務(wù)器產(chǎn)生性能瓶頸的時(shí)候,你可能得想想其它的辦法來(lái)進(jìn)一步提高PHP的速度了。這篇文章將從幾個(gè)方面介紹如何做到這一點(diǎn),從而令用戶瀏覽的時(shí)候更加“爽”。
代碼優(yōu)化
在這里并不想再次告訴你
如何寫(xiě)更干凈的代碼,這一點(diǎn)我想每個(gè)人都清楚,在需要速度的時(shí)候,你可能已經(jīng)在PHP源代碼的優(yōu)化上面做了不少的工作,這里所提出的是,這個(gè)煩瑣的工作可以交由其它工具來(lái)完成。這就是Zend Optimizer,此程序可以從Zend Technologies的網(wǎng)站(http://www.zend.com/)免費(fèi)得到。它的原理很簡(jiǎn)單,通過(guò)檢測(cè)Zend引擎產(chǎn)生的中間代碼,并且優(yōu)化它來(lái)得到更高的執(zhí)行速度。我認(rèn)為優(yōu)化代碼是一項(xiàng)頗煩瑣的工作,而且優(yōu)化后的代碼可能變得難以理解,尤其是當(dāng)你放下該P(yáng)HP程序一段時(shí)間后,突然間客戶要求你做一些修改時(shí),可能你自己也不懂了;-)。因此我建議你在PHP的源代碼較為復(fù)雜的時(shí)候,用Zend Optimizer來(lái)做這個(gè)優(yōu)化的工作,好處是它不會(huì)令你的代碼變得復(fù)雜難懂。
安裝Zend Optimizer是非常簡(jiǎn)單的。只要根據(jù)你使用的平臺(tái),下載相關(guān)的預(yù)編譯庫(kù),并且在你的php.ini中加入兩行,重新啟動(dòng)你的web服務(wù)器就行了!
zend_optimizer.optimization_level=15zend_extension="/path/to/ZendOptimizer.so" zend_loader.enable=Off
你可能有點(diǎn)奇怪,不是說(shuō)兩行嗎,怎么變成三行了。不過(guò)第三行是可選的,看來(lái)禁止這個(gè)zend_loader將會(huì)令優(yōu)化的速度更快,因此不妨在你的php.ini文件中多加這一行。要注意的是:只有在你不使用Zend Encoder Runtime的時(shí)候,才可以禁止zend_loader,關(guān)于Zend Encoder Runtime,還會(huì)在下文提到。
要更快嗎?使用cache(緩沖)吧
如果你的PHP應(yīng)用還需要更快的速度,下一個(gè)辦法是緩沖。要實(shí)現(xiàn)這一點(diǎn),有幾種不同的方式。我自己就試用過(guò)Zend Cache(評(píng)測(cè)版本),APC和Afterburner Cache。
以上提到的都是“緩沖模塊”。它們的原理都差不多,在php文件被首次請(qǐng)求的時(shí)候,通過(guò)將你的PHP源代碼的中間代碼存儲(chǔ)在web服務(wù)器的內(nèi)存中,對(duì)于以后同樣的請(qǐng)求,都直接提供內(nèi)存中的“編譯”版本。由于它可以令磁盤(pán)的訪問(wèn)達(dá)到最小化,因此這個(gè)方法確實(shí)可以極大地提高PHP的性能。更為方便的是,當(dāng)你的PHP源代碼修改時(shí),緩沖的模塊可以察覺(jué)到這些變化,并且重新載入一樣,因此你不必?fù)?dān)心客戶得到的是舊版本的程序。這些緩沖的模塊確實(shí)不錯(cuò),但是我應(yīng)該選用哪一種呢?下面分別介紹一下:
Zend Cache是Zend Technologies的一個(gè)商業(yè)化的產(chǎn)品(它也是免費(fèi)為我們提供PHP引擎和Zend Optimizer的公司)。它確實(shí)不錯(cuò)。在首次運(yùn)行后,你可以明顯察覺(jué)到PHP的速度得到了很大的提高,服務(wù)器的空閑資源也更多了。缺點(diǎn)是你要付費(fèi)購(gòu)買(mǎi)它,但就性價(jià)比來(lái)說(shuō),還是非常值得的。
Afterburner Cache是Bware Technologies(bwcache.bware.it)提供的免費(fèi)緩沖模塊。當(dāng)前還只是beta版,它所做的工作看來(lái)與Zend Cache差不多,不過(guò)性能的提高就比不上Zend Cache,而且現(xiàn)有的版本不能和Zend Optimizer一起工作,不過(guò)它是免費(fèi)的。
APC(Alternative PHP Cache)是由Community Connect(apc.communityconnect.com)提供的另一個(gè)免費(fèi)模塊。它的工作很穩(wěn)定,速度也有不少的提高,要注意的是,我還沒(méi)有找到一個(gè)官方的測(cè)試數(shù)據(jù),這些只是在我的應(yīng)用上作測(cè)試,因此不能下一個(gè)結(jié)論
責(zé)任編輯: xintc110

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

The key steps to install PHP on Windows include: 1. Download the appropriate PHP version and decompress it. It is recommended to use ThreadSafe version with Apache or NonThreadSafe version with Nginx; 2. Configure the php.ini file and rename php.ini-development or php.ini-production to php.ini; 3. Add the PHP path to the system environment variable Path for command line use; 4. Test whether PHP is installed successfully, execute php-v through the command line and run the built-in server to test the parsing capabilities; 5. If you use Apache, you need to configure P in httpd.conf

The basic syntax of PHP includes four key points: 1. The PHP tag must be ended, and the use of complete tags is recommended; 2. Echo and print are commonly used for output content, among which echo supports multiple parameters and is more efficient; 3. The annotation methods include //, # and //, to improve code readability; 4. Each statement must end with a semicolon, and spaces and line breaks do not affect execution but affect readability. Mastering these basic rules can help write clear and stable PHP code.

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

TohandlefileoperationsinPHP,useappropriatefunctionsandmodes.1.Toreadafile,usefile_get_contents()forsmallfilesorfgets()inaloopforline-by-lineprocessing.2.Towritetoafile,usefile_put_contents()forsimplewritesorappendingwiththeFILE_APPENDflag,orfwrite()w

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.
