如果提升PHP程序的性能
一代大神Hoare曾经说过:”过早的优化是一切不幸的根源。“在我是个小菜鸟的时候,根本不知道这句话是否正确,但是当自己成为一个老鸟之后,就发现这句话是相当的犀利,说的恰到好处,也不愧它能够流传这么广泛。
要想优化PHP的性能,第一点要注意的就是缓存,有人说过如果PHP使用了缓存,它的性能会提高五百倍,可惜Zend公司在致力于卖自己的产品,因此一个公司把一门语言给坑了,先不说这个公司的问题,就缓存的重要性而言,是绝不容忽视的。我们可以用缓存模块(比如Memcache)或者缓存模板(Smarty)等来进行一系列的缓存处理,当然我前面介绍的两个页面静态化和文件方式缓存数据库信息的方式也在这一类里面。
第二点就是内存的消耗问题,这点说起来还是比较大的,说几个细节把,就是有些人喜欢把变量赋值一份,从而增大内存开销,比如我们从GET接收过来的参数,直接用就可以了,非要再赋值给另外一个变量,导致在该变量的内存消耗翻倍,比如如下代码:
<?php //只是为了美观的新变量$msg = strip_tags($_GET['msg']);echo $msg;
echo strip_tags($_GET['msg']);
第四点就是在数据库的操作上,有些新手喜欢用到一次数据就查一次,其实我们每次连接数据库再断开连接的开销还是蛮大的,我们应该把要做的sql操作放到一起,统一执行,避免多次的数据库连接和断开。
第五点就是在include或者require文件的时候,使用绝对路径绝对要快很多,另外可以使用自动加载机制,即方便,又保证了效率,关于自动加载,可以看我的一篇博文,今天刚写的。
第六点就是在某些大块头的部分进行调优,很多人喜欢用smarty,如果是大型的网站,还需要考虑到smarty的开销,有人统计过,smarty会占据到百分之十的开销,这点还是非常可观的。
第七点就是使用单引号代替双引号来定义字符串会让速度稍微快一点,因为PHP会在双引号包围的字符串中搜寻变量,但是单引号不会。使用静态方法也提高速度,因为它避免了类的实例化,会节省开销。使用echo输出多个字符信息的时候,使用逗号而不是原点会加快速度,因为它省去了拼接字符串的开销。include文件的时候使用绝对路径也会减小开销,因为它避免了PHP去includ_path里面查找文件的操作,提升性能。
第八点就是对于简单的字符串,不要使用正则表达式,这样会加大开销,使用switch case语句要比多个if。。elseif要优化的多,因为它省去了多重判断的开销。对于变量的操作的时候,递增一个局部变量是最快的,递增一个全局变量要慢大约2倍,递增一个对象的属性大约会慢三倍。
第九点就是我们的静态化的一点原因了,因为Apache解析一个PHP脚本的速度比解析一个静态HTML页面会慢大约2倍到10倍左右,因此,这也是静态化的优点之一。在我们读取文件的时候,能用file_get_contents就使用它,而不是fopen,fread等操作。
最后一点就是foreach的效率比for和while效率高一点,是在想不起来什么其他的了,如果读者能够想到的话,希望补充奥,对了,还有apc这种缓存,其实我第一点说的就是缓存。。。

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

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 have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
