PHP Performance Optimization on Basic Syntax and Core
Here are some performance/optimization tips, tricks and best practices to keep in mind when developing in PHP.
The list will be updated on a regular basis, so check back for updates
Basic Syntax & Core
• Use single quotes instead of double quotes where possible; PHP looks inside double quoted strings for variables , however does not need to do this inside single quotes.
• However, $var = 'hello' . "
"; is SLOWER than $var = "hello
";
• Avoid relative paths; As the system will need to resolve them into absolute paths every time. If you need to use relative paths, define it once and convert it into an absolute path using the realpath() function.
• Avoid <? short tags and try to use <?php full tags; This will save you headaches when moving between environments that do not support PHP Short tags which are environment dependent. Furthermore, all other forms of PHP tags are deprecated.
• Unset() your unused variables, especially large objects or arrays to save memory.
• ++$count is faster than $count++; this may need some verification.
• Avoid declaring unnecessary variables; instead of $var = some_operation(); return $var; we can simply do return some_operation() to save memory.
• Capitalized TRUE, FALSE, NULL and other reserved keywords are not slower or faster to its lower cased counter-parts; I benchmarked this and found no real trend in which one was faster, the results were roughly equal with (expected) sporadic results both ways.
• Similarly, 'AND', 'OR' is neither faster nor slower than '&&', '||'; After benchmarks, there were no noticeable differences between the 2 methods, therefore use the one that is most logical or preferred to you.
Comments 0