PHP 性能优化
原文出处: 川山甲
序
php是一个很流行的脚本语言,现在很多公司(新浪、优酷、百度、搜狐、淘宝等等)在使用这种语言进行网站开发。我的这篇文章,我只是希望能够提高你的php脚本性能。请记住你的php脚本性能,很多时候依赖于你的php版本、你的web server环境和你的代码的复杂度。
优化你代码中的瓶颈
Hoare曾经说过“过早优化是一切不幸的根源”。当你想要让你的网站更快运转的时候,你才应该去做优化的事情。当你要改变你代码之前,你需要做的事是什么原因引起了系统缓慢?你可以通过以下指导和其他方式优化你的php,可能是数据库原因也可能是网路原因!通过优化你的php代码,你能尝试着找出你的系统瓶颈。
升级你的php版本
你的团队成员提出,这些年php引擎已经有很多象征性的性能提升。如果你的web server仍然运行着比较老的版本,如php3或者php4。那么在你尝试着优化你代码之前,应该先深入调查一下版本之间的升级情况。
点击以下链接,可以了解具体细节:
从 PHP 4 移植到 PHP 5
从 PHP 5.0.x 移植到 PHP 5.1.x
从 PHP 5.1.x 移植到 PHP 5.2.x
使用缓存
利用缓存模块(如Memcache)或者模板系统(如Smarty)进行缓存处理。我们可以缓存数据库结果和提取页面结果的方式来提升网站性能。
使用输出缓冲区
当你的脚本尝试着渲染的时候,php会使用内存缓存区保存所有的数据。缓存区可能让你的页面看起来很慢,原因是缓冲区填满所有要响应的数据之后再把结果响应给用户。幸运的是,你能够做一下改变,迫使php强行在缓冲区填满之前把数据响应给用户,这样就会让你的网站看起来更快一些。
- 输出缓存控制
避免写幼稚的setters和getters
当你写php类的时候,你可以直接操作对象属性,这样能帮助你节省时间和提升你的脚本性能。而不是那种让人感到幼稚可笑的setters和getters。
下面是一些案例:dog类通过使用setName()和getName()方式来操作name属性。
class dog { public $name = ''; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; }}
注意:setName()和getName()除了存储和返回name属性外,没做任何工作。
$rover = new dog();$rover->setName('rover');echo $rover->getName();
直接设置和访问name属性,性能能提升100%,而且也能缩减开发时间!
$rover = new dog();$rover->name = 'rover';echo $rover->name;
没有原因不要copy变量
有时初级phper,为了使代码更加“干净”,常常把已经定义的变量重新赋值给另一个变量。这实际上就导致了双重内存的消耗(当改变变量的时候),这就导致脚本的性能下降。比如一个用户把一个512KB的变量在额外插入给另一个变量,那么就会导致1MB的内存被消耗掉。
$description = strip_tags($_POST['description']);echo $description;
上面的代码没有任何原因,复制了一遍变量。你仅需要使用内联的方式简单输出变量,而不用额外的消耗内存。
echo strip_tags($_POST['description']);
避免循环做SQL操作
经常犯的错误是把一个SQL 操作放置到一个循环中,这就导致频繁的访问数据库,更重要的是,这会直接导致脚本的性能低下。以下的例子,你能够把一个循环操作重置为一个单一的SQL语句。
foreach ($userList as $user) { $query = 'INSERT INTO users (first_name,last_name) VALUES("' . $user['first_name'] . '", "' . $user['last_name'] . '")'; mysql_query($query);}
过程:
INSERT INTO users (first_name,last_name) VALUES("John", "Doe")
替换这种循环方案,你能够拼接数据成为一个单一的数据库操作。
$userData = array();foreach ($userList as $user) { $userData[] = '("' . $user['first_name'] . '", "' . $user['last_name'] . '")'; }$query = 'INSERT INTO users (first_name,last_name) VALUES' . implode(',', $userData);mysql_query($query);
过程:
INSERT INTO users (first_name,last_name) VALUES("John", "Doe"),("Jane", "Doe")...
- MySQL INSERT Syntax
其他资源
- PHP Memcache module
- Smarty templating engine
- http://3v4l.org ? 分析各个版本间的代码执行效率,非常不错的网站
- http://www.php-internals.com/ ???研究php内核的网站!
总结
php在性能方面的优化还有很多,如果对这方面有更深入了解的人,可以一起探讨,我会把大家好的建议也放入到博文里面, 供其他phper参考。作为phper能提高众多phper
的能力是一件非常自豪的事情。??很多人都把php当成草根语言,我个人也希望php语言将来能走的更远,这样作为phper手中的money也会越来越多!

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











There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

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.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

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.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

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

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.
