PHP optimize

Why "micro-"? Because changing logic of your application may give you much better performance boost then applying all these tips. But they still can make your code better. You always need to output something, why do not use "echo" instead of "print"?

* calling an object method is faster then traping it with with "__call"

* calling a "static" method is faster then an object method

* calling a function is faster then calling a static method

* accessing a local variable is faster then then a global variable

* accessing a global variable is faster then an object property

* accessing an object property is faster then trapping it with "__get" and "__set"

* accessing an initialized variable is faster then accessing an uninitialized variable

* absolute paths in "include" and "require" are faster then relative

* merging several scripts in one file and then including it is faster then several includes

* "switch" is faster then "if ... else if ..." in some cases

* do not use regexs for simple string processing tasks

* avoid @ (error control operator)

* avoid notices and warnings in your scripts

* avoid unused variables and unused method parameters

* adding method parameter increases calling time

* adding method parameter type hint increases calling time

* unset variables that contain large amount of data or circular references

* $_SERVER['REQUEST_TIME'] contains script startup time

* cache page output or result of resource-consuming functions

* "echo" is faster than "print"

* "echo" accepts several arguments, you can use it instead of string concatenation

* "ob_start()" and "ob_end_clean()" is may be better then several string concatenations

* strings in single quotes ('...') are processed faster then strings in double quotes ("...")

* pre-increment (++$i) is faster then post-increment ($i++)

*"isset" is a faster alternative to "array_key_exists"

* an array is a faster alternative to a class with several fields

* "foreach" is better then "for" in many cases

* open resources (files, database connections, sockets) right before using them, free resources as soon as you do not need them

* do not fetch database fields which you do not use in your script

* use prepared database statements

* combine several queries in one when database supports it

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值