Php Code Optimization tips

1.    Use static methods when possible, this is 4 times faster
2.    Echo is faster as print
3.    Use , instead of . to concatenate a string
echo "Hello", $world;
echo "Hello" . $world;
4.    Set the max value in a for-loop before the loop, not in the if statement.
$max = count($array);
for ($i=0;$i<$max;$i++) 
    echo $i;
5.    Unset vars to clear memory (especially when using arrays)
6.    Use full paths in includes and requires, so the server doesn't have to resolve the paths for you
7.    Use strncasecmp, strpbrk and stripos in stead of regex
8.    “else if” statements are faster than select statements aka case/switch
9.    Suppress errors with an @ is very slow
10.    Close database connections when you don't need them anymore
11.    $row[’id’] is 7 times faster than $row[id], because if you don’t supply quotes it has to guess which index you meant, assuming you didn’t mean a constant.
12.     Wrap your string in single quotes (') instead of double quotes (") is faster because PHP searches for vars in "..." and not in '..'
13.    A PHP script is 2 to 20 times slower as a HTML page. Use HTML pages when possible.
14.    PHP scripts are compiled every time you call them. Use PHP caching software to gain a speed optimization of 25% to 100%.
15.    $i++ is slower as ++$i
16.    Not everything has to be OOP. Most of the time OOP is just overhead, so use it wisely!
17.    Use as much of the default functions of PHP, you don't have te reinvent the wheel!
18.    Turn on error_reporting(E_ALL | E_STRICT); in development.
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

0 comments: on "Php Code Optimization tips"

:) ) ;) ) ;;) :D ;) :p :( ( :) :( :X =(( :-o :-/ :-* :| 8-} :) ] ~x( :-t b-( :-L x( =))
Post a Comment