随着国内PHP开发的兴起,代码优化成了老生常谈的话题了。作者本人时不时也会跑到CSDN坛论与人交流交流有交PHP开发和优化问题,这也是可以使自身快速提高的方法。
程序员在编写程序时,总是想要使自己的程序占用资源最小,运行速度更快,代码量更少。往往我们在追求这些的同时却失去了很多东西。优化的目的是花最少的代价换来最快的运行速度与最容易维护的代码。以下就几点PHP的优化做下总结:
1、in_array的用法
避免在大的数组上使用in_array(),同时避免在循环中对包含20个以上元素的数组使用这个函数。in_array()会非常消耗资源.对于小的数组这种影响可能很小,但是在一个循环中检查大数组可能会需要好几秒钟的时间。如果您确实需要这个功能,请使用isset()来查找数组元素。实际上是使用键名来查询键值。调用isset($array[$var])会比in_array($var,array_keys($array))要快得多。
2、数组键名
在PHP中,使用不经单引号包含的字符串作为数组键名是合法的,但是我们不希望如此--键名应该总是由单引号包含而避免引起混淆。注意这是使用一个字符串,而不是使用变量做键名的情况。
例如:
// 不好的写法 $foo = $assoc_array[blah]; // 好的写法 $foo = $assoc_array['blah']; // 不好的写法 $foo = $assoc_array["$var"]; // 好的写法 $foo = $assoc_array[$var];
3、字符串表达
在PHP中用两种不同的方式来表示一个字符串-单引号或者双引号。主要的区别在于语法解释器会对双引号表示的字符串进行变量替换,而不会处理单引号表示的字符串。因此您应该总是使用单引号,除非您真的需要在字符串中处理变量。这样,我们可以减少程序运行消耗,因为语法解释器不需要每次多处理一大堆根本没有变量的字符串。
同样,,如果您在函数调用中使用了一个字符串变量作为参数,您不需要将这个变量包含在引号里。这会导致语法解释器多做好多无用功。记住,几乎所有双引号中的转义符对于单引号都是无效的。您需要留意以上的规则,但是有时候为了代码的可读性,可以适当的破例。
例如:
// 不好的写法 $str = "This is a really long string with no variables for the parser to find."; do_stuff("$str"); // 好的写法 $str = 'This is a really long string with no variables for the parser to find.'; do_stuff($str); // 有时候单引号不是那么合适 $post_url = $phpbb_root_path . 'posting.' . $phpEx . '?mode=' . $mode . '&start=' . $start; // 双引号有时候能让代码行更集中 $post_url = "{$phpbb_root_path}posting.$phpEx?mode=$mode&start=$start";
4、循环定义中的操作
在比较部分如果存在运算请一定要进行优化。因为这部分会在循环中的每一步进行操作:
// 在每次循环中sizeof函数都要被调用 for ($i = 0; $i
5、分支判断,switchcase好于ifelse
使用分支语句要比用很多ifelse好很多。
6、不要滥用@操作符
不要滥用@操作符,看上去很简单,但实际上用@比不用@效率要差几倍。
特别不要在循环中使用@。
7、获取时间的方法
很多时候我们习惯使用time()获取当前时间,但time()终归是一个函数,不如直接用$_SERVER['REQUEST_TIME']好一点。
8、字符串长度判断
两种写法:
(1)if(strlen($str)
(2)if(isset($str[5])){ /* do something */ }
以上是小编在网上整理的php优化技巧,希望对大家的学习有所帮助!

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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
