2

I have several pages which use the include or require language constructs within PHP. Many of these lie within IF, ELSE statements.

I do realize that a page will not load at all if a require'd file is missing but the main purpose of including this way is to:

1) reduce code clutter on the page

2) not load the file unless a statement is met.

Does issuing an include or require statement load the file regardless (and thus eliminating the benefits I was trying to achieve by placing within the if/else statement?

Brief example:

<?php


$i = 1

if($i ==1) {

      require_once('somefile.php');

} else {

     require_once('otherfile.php');
}

?>

At page load, are both files checked AND loaded?

NikiC
  • 100,734
  • 37
  • 191
  • 225
JM4
  • 6,740
  • 18
  • 77
  • 125

4 Answers4

9

If you place an include/require statement in the body of an if (or else), it'll be executed if the if's condition is true.

if ($a == 10) {
    // your_first_file.php will only be loaded if $a == 10
    require 'your_first_file.php';
} else {
    // your_second_file.php will only be loaded if $a != 10
    require 'your_second_file.php';
}


And, if you want, you can test this pretty easily.
This first example :

if (true) {
    require 'file_that_doesnt_exist';
}

will get you :

Warning: require(file_that_doesnt_exist) [function.require]: failed to open stream: No such file or directory
Fatal error: require() [function.require]: Failed opening required 'file_that_doesnt_exist'

i.e. the require is executed -- and fails, as the file doesn't exist.


While this second example :

if (false) {
    require 'file_that_doesnt_exist';
}

Will not get you any error : the require is not executed.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • Hahahaha! Pascal Martin is at it again - with excellent and detailed input, as always. :) +1 for showing how to test it – Pekka Mar 12 '11 at 22:46
4

At page load, are both files checked AND loaded?

No, at least not since (IIRC) PHP 4.0.1.

If you want to reduce include clutter, and you are working with mainly object-oriented code, also take a look at PHP's autoloading.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • thanks! I just didn't want to waste unnecessary resources and have always wondered – JM4 Mar 12 '11 at 22:36
  • Whee are some good reasons against autoloading - most related to bytecode caches. – ThiefMaster Mar 12 '11 at 22:37
  • 2
    @ThiefMaster: I don't follow your reasoning (well, there isn't any). I use APC with enabled opcode cache and my applications usually make heavy use of autoloading, but I never had any problems with it. What am I doing wrong? What should I do to get problems? – NikiC Mar 12 '11 at 22:45
  • I don't remember what exactly caused it.. but I think I read about it here on SO so a search might help. – ThiefMaster Mar 12 '11 at 23:03
  • 1
    Autoloaders and opcode caches used to not get along. This is no longer the case in modern PHP (since the realpath cache was created), though poorly-designed autoloaders can cause more filesystem poking and prodding than strictly necessary. – Charles Mar 12 '11 at 23:08
2

No, only one of those files would be loaded.

dialer
  • 4,348
  • 6
  • 33
  • 56
2

The include and require constructs are only evaluated when passed. The files are only read when your expression is met.

It's simple to explain by considering that the constructs might contain variables:

require_once("otherfile-{$i}.php");

That's supported. But it could not possibly work before PHP runs over that particular line, because it needs to know the state of $i to load the right file.

mario
  • 144,265
  • 20
  • 237
  • 291
  • Hopefully someone copies you scripty and replaces the `{$i}` with a `{$_GET['i']}` :D (Yes, I am evil, hehehe) – NikiC Mar 12 '11 at 22:48
  • Your advises will yield you an unpleasant afterlife. – mario Mar 12 '11 at 23:04
  • Oh, who knows. Probably you just need to say `' OR 1 --` just before they send you to hell and you'll magically land in paradise ;) +1 – NikiC Mar 12 '11 at 23:11