iEntry 10th Anniversary Add News Photoshop Tutorials


admin

10 Top Ways To Optimize Your PHP Code

Aug 11, 2009 by admin

There are actually many more ways to optimize your PHP code, though from tests, these are the top 10 that you should not omit from any PHP project. It’s important to optimize your scripts to reduce loading times locally, load times on servers and for a general balance between the PHP operation and the static result.

Many projects can have dozens of PHP scripts. If the code in those scripts has not been optimized, slower behavior will be noticed and reported. Database applications are prone to PHP slowdowns. Also timeouts can cause havoc in a commercial environment if the PHP code has not been optimized.

Here are 10 good tips on optimization:


  1. Be careful with concatenation as using it too much can cause a reduction in speed. Use echo properly as opposed to concatenation with the dot operator, use echo like this:

echo ‘Hello from ‘,$name,’string’;


  1. You may be used to writing absolute paths into hyperlinks and it’s a good idea to keep writing full paths inside your include statements. This cuts the time spent resolving the file path.

  2. When adding your data structures, you may wish to use classes as this is usually the one thing in PHP scripts that helps organize the script. Though, look at the number of structures you have, can some of them be written as arrays?

  3. Specify the key type when accessing array elements from an echo statement like this:

echo $array[‘key’];


  1. Look through your scripts for large declared arrays. These can become quite large in some instances so unset the variables used in these arrays to free up memory for other operations.

  2. Any static methods in your code? Are they declared as being static? If not, declare them as static as this will improve the script loading speed.

  3. An apache module called mod_gzip can compress your data so that it transfers to and from database tables much quicker.

  4. This may seem obvious to any programmers so apologies if so – are you closing your database connections once finished with those connections?

  5. How many ‘multi if’ and ‘else if’ statements are you using? Use ‘select’ in various places as this helps to optimize performance.

  6. On the subject of copious numbers of PHP scripts, it becomes too easy to just write the PHP page. You can end up with so many PHP scripts where a HTML static page can be done instead. Remember, PHP scripts run at slower speeds than HTML pages. This is a function of Apache.

Hope this list helps to optimize your PHP coding life.

Popularity: 2% [?]

About The Author

Related Posts

Don't miss any post, Subscribe to our to stay up to date
No comment yet, be the first!

Add Your Comment