Home php教程 php手册 优化PHP代码技巧的小结

优化PHP代码技巧的小结

Jun 13, 2016 am 11:52 AM
php code optimization statement Skill method of static

优化PHP代码技巧的小结
1. 如果一个方法能被静态,那就声明他为静态的,速度可提高 1/4;
2. echo 的效率高于 print,因为 echo 没有返回值,print 返回一个整型;
3. 在循环之前设置循环的最大次数,而非在在循环中;
4. 销毁变量去释放内存,特别是大的数组;
5. 避免使用像__get, __set, __autoload 等魔术方法;
6. requiere_once()比较耗资源;
7. 在 includes 和 requires 中使用绝对路径,这样在分析路径花的时间更少;
8. 如果你需要得 sexinsex 到脚本执行时的时间,$_SERVER['REQUSET_TIME']优于 time();
9. 能使用字符处理函数的,尽量用他们,因为效率高于正则;//
10. str_replace 字符替换比正则替换 preg_replace 快,但 strtr 比 str_replace 又快 1/4;
11. 如果一个函数既能接受数组又能接受简单字符做为参数,例如字符替换,并且参数列表
不是太长,可以考虑多用一些简洁的替换语句,一次只替换一个字符,而不是接受数组
做为查找和替换参数。大事化小,1+1>2;
12. 用@掩盖错误会降低脚本运行速度;
13. $row['id']比$row[id]速度快 7 倍,建议养成数组键加引号的习惯;
14. 错误信息很有用;
15. 在循环里别用函数,例如 For($x=0; $x 16. 建立一个全局变量要比局部变量要慢 2 倍;
17. 建立一个对象属性(类里面的变量)例如($this- >prop++)比局部变量要慢 3 倍;
18. 建立一个未声明的局部变量要比一个初始化的局部变量慢 9-10 倍;
19. 明一个未被任何一个函数使用过的全局变量也会使性能降低( 和声明相同数量的局部变
量一样),PHP 可能去检查这个全局变量是否存在;
20. 方法的性能和在一个类里面定义的方法的数目没有关系,因为我添加 10 个或多个方法
到测试的类里面(这些方法在测试方法的前后)后性能没什么差异;
21. 在子类里方法的性能优于在基类中;
22. 只调用一个参数并且函数体为空的函数运行花费的时间等于 7-8 次$localvar++运算,而
一个类似的方法(类里的函数)运行等于大约 15 次$localvar++运算;
23. 当输出字符串时用逗号代替点分割更快些。注意:这只对 echo 起作用,这个函数能接
受一些字符串作为参数;
24. 在 apache 服务器里一个 php 脚本页面比相应的 HTML 静态页面生成至少要多花 2-10 倍
的时间,建议多用些静态 HTML 页面和少量的脚步;
25. 除非你的安装了缓存,不然你的 php 脚本每次被访问都需要被重编译。建议安装个 php
缓存程序,这样通过去除一些重复的编译来很明显的提高你 20-100%的性能;
26. 建议用 memcached,高性能的分布式内存对象缓存系统,提高动态网络应用程序性能,
减轻数据库的负担;
27. 使用 ip2long()和 long2ip()函数把 IP 地址转成整型存放进数据库而非字符型。这几乎能降
低 1/4 的存储空间。同时可以很容易对地址进行排序和快速查找;
28. 使用 checkdnsrr()通过域名存在性来确认部分 email 地址的有效性,这个内置函数能保证
每一个的域名对应一个 IP 地址;
29. 如果你在使用 php5 和 mysql4.1 以上的版本,考虑使用 mysql_*的改良函数 mysqli_*;
30. 试着喜欢使用三元运算符(?:);
31. 在你想在彻底重做你的项目前,看看 PEAR 有没有你需要的。PEAR 是个巨大的资源库,
很多 php 开发者都知道;
32. 使用 highlight_file()能自动打印一份很好格式化的页面源代码的副本;
33. 使用 error_reporting(0)函数来预防潜在的敏感信息显示给用户。理想的错误报告应该被
完全禁用在 php.ini 文件里。可是如果你在用一个共享的虚拟主机, php.ini 你不能修改,
那么你最好添加 error_reporting(0) 函数,放在每个脚本文件的第一行 ( 或用
require_once()来加载)这能有效的保护敏感的 SQL 查询和路径在出错时不被显示;
34. 使用 gzcompress() 和 gzuncompress()对容量大的字符串进行压缩(解压)在存进( 取出)数
据库时。这种内置的函数使用 gzip 算法能压缩到 90%;
35. 通过参数变量地址得引用来使一个函数有多个返回值。你可以在变量前加个“&”来表示
按地址传递而非按值传递;
36. 使用 strlen()因为要调用一些其他操作例如 lowercase 和 hash 表查询所以速度不是太好,
我们可以用 isset()来实现相似的功能,isset()速度优于 strlen();

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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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

Java Tutorial
1659
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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 in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

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.

See all articles