- If a method can be static, declare it static, speed can be improved.
- Echo is more faster than print.
- Use Echo’s multiple parameters instead of string concatenation.
- Unset your variables to free memory, especially large arrays.
- Avoid magic like __get, __set, __autoload.
- Require_once() is expensive.
- Use full paths in includes and requires, less time spent on resolving the OS paths.
- Str_replace is faster than preg_replace, but strtr is faster than str_replace
- Error suppression with @ is very slow.
- Close your database connections when you’re done with them
- Error messages are expensive
- Incrementing a global variable is 2 times slower than a local var.
- Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable.
- Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one.
- Use any frame works (For example cake php,yii frame work)
- Use Ternary Operators in your coding
- Methods in derived classes run faster than ones defined in the base class.
- Use if else conditions are more faster than the select statements.
- Avoid looping in your coding.When you use array go for the foreach loop rather than for loop.
- Don’t use short tags in your coding.
- ++$i is more faster than $ i++ ,use pre-increment when it is possible.
- Avoid the PHP mail() function header injection issue.
- $row[’id’] is 7 times faster than $row[id], because if you don’t supply quotes it has to guess which index you meant, assuming you didn’t mean a constant.
- Separate code, content and presentation: keep your PHP code separate from your HTML.
- Avoid using plain text when storing and evaluating passwords to avoid exposure, instead use a hash, such as an md5, hash.
- Not everything has to be OOP, often it is just overhead, each method and object call consumes a lot of memory.
Home/ Blog