Tips Tricks Tutorials

WEB-NES-BAY

Learn Tips and tricks on Linux, Hacking, linux, PHP, Perl, Web, Hardware

bookmark bookmark
WEBNESBAY On October - 15 - 2009

Here are the 10 php optimization tips which I have identified with my past programming experiene in php. This optimization tips helps you in increasing the performance of your php application.

Declare static

If you want to make a method as static, do it and declare it as static. It improoves the speed by a factor of 3 or 4. I have tested this with my php applicaiton and i have seen this speed factor

Use full path

If you are including files in a php program, use full paths. This optimization tip helps  spent less time in resolving the OS Paths.

require_once()

Using require_once () is more expensive. Try to avoid using this function.

Avoid Magic methods

Try to avoid using magic methods like __sleep __wakeup __get __set.  Using this methods creates tight dependency between model objects and database schemas.

Avoid functions in loops

Do not use the functions inside of for loop such as for($1=0;$i<function(#fix);$i) function()  gets called for every time.

Request time

If you need to find out the time when the script started executing (helps in debugging), $_SERVER['REQUEST_TIME'] is preferred to time(). Try to use $_SERVER variables to get instead of methods

Avoid Regular expressions

Try to use use strncasecmp, strpbrk and stripos instead of regular expressions.  For example str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor 4

Passing Arguments

If the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments

Error Handling

Error messages are very expensive as it deals with IO handling, so error messages are very expensive. Error suppression with @ is very slow

Retrieving array items

Use single quotes for retrieving array items. For example: $array['id'] is 6 times faster than $array[id].

I hope that this tips will help you in speeding up your php applications. Happy Programming :-)

No related posts.

Categories: PHP

2 Responses

  1. I’m sure you’re a nice guy, but micro-optimizations like these… It’s what good php programmers avoid. Search ‘php micro optimizations’ on google and see the debate. :)

  2. ryan says:

    using static methods is very bad. its not good oo and it makes testing nearly impossible. also, agree 100% with what the above poster said about micro optimizations.

Leave a Reply