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:
-
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’;
-
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.
-
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?
-
Specify the key type when accessing array elements from an echo statement like this:
echo $array[‘key’];
-
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.
-
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.
-
An apache module called mod_gzip can compress your data so that it transfers to and from database tables much quicker.
-
This may seem obvious to any programmers so apologies if so – are you closing your database connections once finished with those connections?
-
How many ‘multi if’ and ‘else if’ statements are you using? Use ‘select’ in various places as this helps to optimize performance.
-
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
- 9 Top CSS Essential Skills That Every Web designer Should Learn
- PHP and MySQL Security: How to Protect Against SQL Injections
- PHP Tutorials Utopia: 13 Vital PHP skills for every novice PHP developer and solutions
- How To Create An Article Directory using PHP
- Internet Explorer 8: Microsoft Starting to Comply with Industry Standards
Add Your Comment