Another Ten PHP Optimization Tips

| No Comments | No TrackBacks
I wrote PHP Optimization to improve performance. Now I have another ten PHP optimization tips for you.
1. Single-quoted strings.
Use single quote when possible. It is faster than double quote. If it is string only, just pick single quotes.
2. The way output data.
Could you point which is the fastest way to output from below?
print "Hi my name is $a. I am $b";
echo "Hi my name is $a. I am $b";
echo "Hi my name is ".$a.". I am ".$b;
echo "Hi my name is ",$a,". I am ",$b;
The last one is actually the fastest operation.
3. Use single-quotes around array indexes.
So, $x['sales'] is alway best format and fast.
4. Don't use short open tags.
<?php is formal tag.
5. Use regular expressions only when you really need it.

When doing string operation, like replace part of string. strtr is the fastest.
str_replace is faster.
preg_replace is slow.
6. Don't use functions inside a loop declaration.
Bad:
 
for ($i = 0; $i < count($array); $i++) {
//stuff
}
Good:
 
$count = count($array);
for($i = 0; $i < $count; $i++) {
//stuff
}
7. Never rely on register_globals or magic quotes.
When building new program, do not use it.
The next three is a good habit for coding, not only for PHP, but to all language coding.
8. Always initialize your variables.
9. Document your code
10. Code to a standard.

Related Articles

No TrackBacks

TrackBack URL: http://www.yinfor.com/mtcgi/mt-tb-0822.cgi/2184

Leave a comment

About this Entry

This page contains a single entry by David Yin published on June 12, 2008 12:38 PM.

Microsoft Security Patch for June 2008 was the previous entry in this blog.

GNOME 2.22 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.21-en

Online Tools