Diposting pada tanggal 25 October 2011
There are few coding practices that developers can adopt to improve
the performance of their PHP based projects. Better scripts execute
faster and reduce server load as well. Thus, reducing page load time.
So, the argument is how to write a better script and which script is
better. Some will argue that PHP is an interpreted language and has its
own limitations. I am listing a few of the performance tips; though some
are very common and are application to any programming language while
others are universally applicable to web based projects in almost any
server side scripting language.
Use Switch and Avoid Multi if-elseif statements
Well,
we all have been taught this in our colleges. But at times, either we
ignore or the cases keep evolving during the course of development and
we end up with a multiple if-elseif set of conditions. If-elseif should
be adopted for 2 or a maximum of 3 conditions. Beyond that Switch
statement is the way to go. It is designed to handle many conditions so
it is advised to use a Switch instead of multiple if-else.
| 1 |
//Ignore Multiple If-ELSEIF |
Use mysqli instead of mysql extension
For
dynamic websites, database operations are prone to take considerable
amount of execution time. Most of the PHP based sites are backed by
MySQL databases. Working on MySQL 5+, it is recommended to use PHP’s
mysqli extension instead of good old mysql extension. mysqli extension
has been developed for PHP to get more out of MySQL.
Function inside Loop declaration
The
use of functions within loop statements eats away considerable amount
of time. The used function is evaluated each time the loop is
incremented. And, its really not worth it when the function is returning
the same value.
| 3 |
for($i==0;$i<count($votes);$i++) { |
| 10 |
$vote_count = count($votes); |
| 12 |
for($i==0;$i<$vote_count;$i++) { |
Single and Double Quotes
Most
of us use single and double quotes alternatively without realising the
purpose and use of the two. Whenever we use a double quote in output
statement, PHP actually takes a look at the content to find any
variables in the string.While a single quote is outputted straightaway
without any lookups.
| 1 |
// Not recommended to use double quotes in static output |
| 3 |
echo "There are no variable in this statement"; |
| 5 |
echo 'There are no variables in this statement'; |
| 9 |
echo 'The output includes the name variable having value '.$name.' variable'; |
Moreover, a comma separated concatenation will work faster than a dot seperated concatenation.
Not recommended, yet double quotes can be used with variable names as
| 1 |
echo "The output includes the name variable having value $name  variable"; |
| 2 |
// With a variable that is interpreted at runtime and replaced by its value. |
Use Full or absolute Path
Most
of us have seen and used the file paths declared with “../” or “../../”
kind of paths. These declarations are said to be using relative paths.
The relative paths in PHP code means that PHP has to interpret, resolve,
locate and then take suitable action. The action can be including some
file.
Unset heavy variables
It
is always a good practice to unset heavy variables like huge arrays.
Unsetting few variables from a page/method containing too many variables
can release some of the memory.
| 1 |
// $cities array may contains large list of cities |
| 3 |
foreach ($cities as $city) { |
| 9 |
// After the work is done remove it from memory |
Avoid print, use Echo
Echo is not a standard PHP function like print. Echo is infact, a language construct and it executes about 8% faster than print.
Stick
to coding standards, use caching options and dont forget, Facebook uses
PHP. Its just the right kind of coding and optimization which builds
great scalable sites.
Versi cetak