Tips to optimize PHP codes
August 13, 2008 2 comments
All the modern programming languages were developed and are made to enable do the same task of many different ways. This "all roads lead to Rome" of modern programming is quite useful, since the developer has the opportunity to choose which jeitos vai guide their programming and follow paths which to implement their projects. With the PHP could not be different: the plan with PHP as well as being quiet and it is possible to implement various solutions in various different ways.
But all this freedom, if misused, can damage the performance and quality of a project. Through bad practices programming PHP someone can make the performance of an entire system fall short of its potential if it had been programmed so "appropriate". There are some small tips that are easy to follow - not interfere in any way the logic of the programme and modus operandi of progamador PHP - and surtem good results, towards the optimization ç ã and the gain in performance and performance of their scripts .
Optimization of output data
The PHP allows the processing server side of soliticações made on the side of the client. It is very important, then, a good performance to show the result of this processing done on the server side, since the front end of an application or system must be easy to use and quick to respond to requests.
Use "echo" instead of "print"
Among the two functions that allow the display of information - the "this" processing server-side - the functions echo and print are the best known. Meanwhile, use the echo is better because, even though both serve and take the same thing - display information and exits - the performance of echo is better.
So prefer using echo ()!
Single Quote and quotes
To display on screen the contents of a variable, you can use the double ( ") and write the variable along with the rest of the display, or use single quotes ( ') and concatenate the variables (.) With the rest of the exit, some way "isolating" the variables in the rest of the display screen.
It is best to use single quotes with "isolation" of variables rather than quotes the variables along with the rest of the content. So you prefer to use:
/ / Way less efficient echo "This is a less efficient way of displaying a $ var!", / / More efficient echo 'This is a most effective way to show one. "$ Var.'! '; And there is still a way quickly and with better performance in PHP. Despite being a little strange this syntax - at least for me - the move results in multiple parameters to the function rather than hold concatenation are more satisfactory:
/ / Very efficient way (with syntax strange ...) echo 'much more efficient way to display a', $ var, '!'; Optimization of loops
There is talk that in making a web site or system in PHP without talking about loops. And since they are so used - precisely because they are so essential - is more important than doing loops PHP of great performance.
Specify in advance to a variable loops for "
A very common thing to do when Programming in PHP is to get the amount of "anything" and, with it, perform a loop is to hold another "anything." And there are many developers who, for lack of knowledge or pregui ç to make this:
for ($ i = 0; $ i <= mysql_num_rows ($ result), $ i ) ([code]) Where, actually, it brings in an extra processing for each itera ç the ã. Specifying in advance and putting a variable how many times the looping vai acontercer, cargo processing is lower and, consequently, your code is optimized:
$ total = mysql_num_rows ($ result); for ($ i = 0; $ i <= $ total, $ i ) ([code]) Optimization of variables and functions
Needless comment on the importance of variables and functions in a PHP script, for that is simple. Optimize functions and variables that it is more necessary if we are seeking an excellent performance. These tips are fast and efficient:
- Using require instead of require_once;
- Use paths absolute rather than relative in "includes", "requires" and the like is better (although it may generate some inconvenience future, for obvious reasons);
- "Else / if" is faster than "switch / case";
- Increasing a local variable is almost 100% slower when not to specify in advance.
More on optimization of codes PHP
As one might imagine that the "tradition" that has the PHP and the extent that it is characteristic, these quick tips not close the subject. For many other tips on how to optimize their codes in PHP, consider reading the following articles (in English):
- PHP Optimization Tips;
- 50 PHP optimisation tips revisited;
- 40 Tips for optimizing your php code;
- The HOWTO on Optimizing PHP;
- 10 things you (probably) did not know about PHP.
And you, has some advice for optimizing your PHP code?














Trackback on August 13, 2008
Trackback on August 13, 2008