1. echo is faster than print.
2. require_once() is expensive
3. If you need to find out the time when the script started executing, $_SERVER['REQUEST_TIME'] is preferred to time()
4. str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4
5. It's better to use select statements than multi if, else if, statements.
6. Close your database connections when you're done with them
7. $row['id'] is 7 times faster than $row[id]
8. Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time.
9. When echoing strings it's faster to separate them by comma instead of dot. Note: This only works with echo, which is a function that can take several strings as arguments.
10. Do NOT use SQL wildcard select. eg. SELECT *
11. Single-quoted strings.
Use single quote when possible. It is faster than double quote. If it is string only, just pick single quotes.
12. The way output data.
Could you point which is the fastest way to output from below?
view sourceprint?1.print
a. print "Hi my name is $a. I am $b";
b.echo "Hi my name is $a. I am $b";
c.echo "Hi my name is ".$a.". I am ".$b;
d.echo "Hi my name is ",$a,". I am ",$b;
The last one is actually the fastest operation.
13. Use single-quotes around array indexes.
So, $x['sales'] is alway best format and fast.
14. Don't use short open tags.
15. 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.
16. 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.
17. Always initialize your variables.
18. Document your code
19. Code to a standard.
20. reduce your include_path to a single entry or don't use it at all
21. include or require absolute paths
22. ensure that each try to include a file is a hit
23. avoid file_exists(), is_file(), is_dir(), is_link() etc.
24. avoid autoloading
Thursday, September 2, 2010
Subscribe to:
Comments (Atom)