How to improve page load speed of your php website?

1. Use include at the place of include_once for including files

2.Remove Blank Spaces from your php code.You can use this function:

function removeBlankLines($string)
return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string);

3. Use , (comma) at the place of . (period) and single quote(') at the place of double quote(") wherever possible in echo statements.
For ex.
echo "my name is". $this->name."and your is".$this->your_name;
echo 'my name is',$this->name,'and your is',$this->your_name;

4. Provide width and height of image tags. For eg width="10" height="10"

5. Use CDN for external Javascript files.

6. If you are using jquery or any other popular js technologies, then use google library service

7.Compress your css with css compressor.

8. Use minified version of javascript libraries, if not available then create one for yourself using sites like javascript compressor.

9.If possible write css files on page itself i.e inline, if file is too large then write css for visible content first on webpage and import css file in footer.

10. Import Javascript files in footer and not in header.

11. Load Javascript and other content Asynchronously or with the help of Ajax.

12. For image which are not above the fold, you can use lazyload javascipt to delay their loading.Read more about Lazy Load Plugin.

13. Compress your images using Yahoo Smush it

14. Use Gzip for compressing webpage.
Use this code before doctype to enable gzip compression on linux servers
 if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();

15. Remove errors and warning from your code.

16. Use only required variables, if possible use values instead of variable.
For ex.
for($i=1;$i<$j;$i++) {   } // do something
for($i=1;$i<10 do="" font="" i="" nbsp="" something="">

17. Use microtime function of php to measure time taken by php to execute any specific function. For eg. if I want to measure time taken by first_function, I can measure it with the following code:
$time_start = microtime(true);
// Call your function
$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Total time to execute first function is $time seconds\n";

18. Use HTTP Caching : For PHP Website you can use header function or mod_expires

19. Avoid using SQL queries in loops(While or For), instead of that use array inside the loop and use implode function to append that array in SQL query.

20. Optimize CSS by using common declarations: 
h1 { padding-top: 10px; }
p { padding-top: 10px; }
h1, p { padding-top: 10px; }

21. Use complex query instead of multiple queries to reduce number of requests.
Select * from products where pid=1;
Select * from text where pid=1; 
Select * from products,text where =

22. You can use mysql procedure's at the place of php function. It will enhance your website security and performance.

No comments:

Post a Comment