原文出处: 川山甲
序
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也会越来越多!

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.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

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 a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function