Search code examples
phpperformance

PHP: What gives faster execution time, include_once or if statement?


I'm creating a theme for my client and they are really picky on the page load time.

So I've thought that the less code will help the page load faster, and I come across the php code to include once.

<?php
include_once "a.php"; // this will include a.php
?>

and if I do with the if statement to include once I have to declare a variable and change the variable to false after the second check

What will be the most efficient coding and help page performance?

I also want to know if there is even better way of coding to help to make the page load faster when we want to execute the code from a file.

Thanks


Solution

  • It might have been true in the dim and distant past that include_once was a lot slower than include, but that was the dim and distant past. PHP's include_once functionality has been optimized heavily since then. Unfortunately, there's still lots of old articles floating around on the internet that make the claim that include_once is slow, even though it's no longer true.

    Even if include_once was a lot slower than include, the odds are it wouldn't cause an appreciable performance impact unless you were including thousands and thousands of files. Investing time on speeding it up is a , especially if you have no evidence that it's a bottleneck in your code.

    First and foremost in any project is getting the code to work to specifications. Code that's slow but works is still better than code that's fast but doesn't work. Once you've got the code working and passing all its unit tests (you are using unit tests, right?) then you can start worrying about performance. And when you get to that point the first thing you should do is profile your code to discover where the actual bottlenecks are, not start guessing at where you think they might be.