search
HomeBackend DevelopmentPHP Tutorial让的PHP代码飞起来的40条小技艺(提升php效率)

让的PHP代码飞起来的40条小技巧(提升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;
[email protected];
13.$row['id']比$row[id]速度快7倍,建议养成数组键加引号的习惯;
14.错误信息很有用;
15.在循环里别用函数,例如For($x=0; $x 16.在方法里建立局部变量速度最快,97xxoo几乎和在方法里调用局部变量一样快;
17.建立一个全局变量要比局部变量要慢2倍;
18.建立一个对象属性(类里面的变量)例如($this->prop++)比局部变量要慢3倍;
19.建立一个未声明的局部变量要比一个初始化的局部变量慢9-10倍;
20.声明一个未被任何一个函数使用过的全局变量也会使性能降低(和声明相同数量的局部变量一样),PHP可能去检查这个全局变量是否存在;
21.方法的性能和在一个类里面定义的方法的数目没有关系,因为我添加10个或多个方法到测试的类里面(这些方法在测试方法的前后)后性能没什么差异;
22.在子类里方法的性能优于在基类中;
23.只调用一个参数并且函数体为空的函数运行花费的时间等于7-8次$localvar++运算,而一个类似的方法(类里的函数)运行等于大约15次$localvar++运算;
24.Surrounding your string by ‘ instead of ” will make things interpret a little faster since php looks for variables inside “…” but not inside ‘…'. Of course you can only do this when you don't need to have variables in the string.
25.当输出字符串时用逗号代替点分割更快些。注意:这只对echo起作用,这个函数能接受一些字符串作为参数;
26.在apache服务器里一个php脚本页面比相应的HTML静态页面生成至少要多花2-10倍的时间,建议多用些静态HTML页面和少量的脚步;
27.除非你的安装了缓存,不然你的php脚本每次被访问都需要被重编译。建议安装个php缓存程序,这样通过去除一些重复的编译来很明显的提高你20-100%的性能;
28.建议用memcached,高性能的分布式内存对象缓存系统,提高动态网络应用程序性能,减轻数据库的负担;
29.使用ip2long()和long2ip()函数把IP地址转成整型存放进数据库而非字符型。这几乎能降低1/4的存储空间。同时可以很容易对地址进行排序和快速查找;
30.使用checkdnsrr()通过域名存在性来确认部分email地址的有效性,这个内置函数能保证每一个的域名对应一个IP地址;
31.如果你在使用php5和mysql4.1以上的版本,考虑使用mysql_*的改良函数mysqli_*;
32.试着喜欢使用三元运算符(?:);
33.在你想在彻底重做你的项目前,看看PEAR有没有你需要的。PEAR是个巨大的资源库,很多php开发者都知道;
34.使用highlight_file()能自动打印一份很好格式化的页面源代码的副本;
35.使用error_reporting(0)函数来预防潜在的敏感信息显示给用户。理想的错误报告应该被完全禁用在php.ini文件里。可是如果你在用一个共享的虚拟主机,php.ini你不能修改,那么你最好添加error_reporting(0)函数,放在每个脚本文件的第一行(或用require_once()来加载)这能有效的保护敏感的SQL查询和路径在出错时不被显示;
36.使用 gzcompress() 和gzuncompress()对容量大的字符串进行压缩(解压)在存进(取出)数据库时。这种内置的函数使用gzip算法能压缩到90%;
37.通过参数变量地址得引用来使一个函数有多个返回值。你可以在变量前加个“&”来表示按地址传递而非按值传递;
38.Fully understand “magic quotes” and the dangers of SQL injection. I'm hoping that most developers reading this are already familiar with SQL injection. However, I list it here because it's absolutely critical to understand. If you've never heard the term before, spend the entire rest of the day googling and reading.
39.使用strlen()因为要调用一些其他操作例如lowercase和hash表查询所以速度不是太好,我们可以用isset()来实现相似的功能,isset()速度优于strlen();
40.When incrementing or decrementing the value of the variable $i++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages, so don't go modifying your C or Java code thinking it'll suddenly become faster, it won't. ++$i happens to be faster in PHP because instead of 4 opcodes used for $i++ you only need 3. Post incrementation actually causes in the creation of a temporary var that is then incremented. While pre-incrementation increases the original value directly. This is one of the optimization that opcode optimized like Zend's PHP optimizer. It is a still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are plenty of ISPs and servers running without an opcode optimizer.
详细出处参考:http://www.jb51.net/article/23019.htm

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

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: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

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.

See all articles

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment