PHP Basics

Easily cache pages with PHP

If you are not using a CMS or a framework you may need to cache your pages to speed up the page loading times. This can easily be done in PHP using the ob functions.

OB Function In PHP

  • ob_start()
  • ob_get_contents()
  • ob_end_flush()

This starts off by setting the the filename of the cached file, then we need to set the time we want to cache the file in the below example we are setting the amount of time to 10 hours.

Then it will check to see if the file has a cached version and the cache time is within the 10 hours, then it will display the file and exit out of the function.

If we need to create a new cache version then we need to use the ob_start() function and then we can display the HTML for the web page, then we use the ob_get_contents to write to a new cache file.

That is it we can now save the cache file to get at a later date.

Easy Page Caching in PHP

Back to top

Gain access to all tutorials

  • Premium tutorials covering WordPress, Laravel and VueJS
  • Download premium content
  • Download premium WordPress plugins
  • Only $5 a month

Join Paulund