tags:

views:

61

answers:

1

What should I use in the following statement? Include or required.

if(a ==b){
 require 'requiredfile.php';
} else {
 require 'requiredfile_2.php'
}

If in a function, I know that one, either include or require, only includes the file when called, the other one will include the file regardless. Am I correct?

+8  A: 

The difference between include and require is that include will only emit a warning when the file is not found, and require will terminate with a fatal error.

If you are loading vital program parts, you probably want to go with require.

Manual

require() is identical to include() except upon failure it will also produce a fatal E_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue.

Pekka
Just want to add on, if you only ever want the file included once, like if it contains function or class definitions, you would likely want to use `include_once` or `require_once`. This makes sure that, even if the file is told to be included 10 times, it's only ever included once, getting rid of "Function already declared" errors.
Slokun
-1 vote. If the include/require fails then you should handle the event gracefully in your code.Also conditionally including code/content is messy. By all means use the autoloader - but included files should never contain inline code / content - it should only generate output / significant state change when specifically asked to do it by the code which included it.
symcbean
Yes, but my understanding is that if the include or require, don't remember which one, if is inside of a function it will load the data when the page is called, while the other will wait to load the data until the function is called. That is what I'm trying to get, which of the two is the one that loads the data once the function is called?
Ole Media
@Ole can you clarify what you mean? I'm not sure I follow you. What are you trying to achieve?
Pekka
Sometime ago I read that is you have a function with a require or include, the file will not be included until the function is called. I don't remember if it was the include or the require one. What I'm trying to figure out is, which of the two is the one that acts this way? When calling a php file that has function with an include 'filename.php' I don't want the include anything until I call the function, unless php already works this way. Meaning that php will not include anything that is inside functions until they are called.
Ole Media
@Ole I think you are referring to an old behaviour that already included the include file even though it was not clear yet whether the condition leading to its inclusion would even be fulfilled. AFAIK, that behaviour was abandoned at some point - 4.2? 4.3? I don't really remember. Anyway, it should work fine now (Assuming you're running on PHP 5.)
Pekka
I'm thinking that when php engine reads a php file, whenever it founds an include it will call the file to be included, regardless if the include is located within a function or not. My goal is to avoid include unnecessary files depending to the condition if ... else, and I believe that either include or the require does this. I may be wrong, and this is why I ask. if(a=b) include HTML A else include HTML B
Ole Media
@Ole why are you thinking that? Your thinking is not correct. Try it out. :)
Pekka
That is what I wanted to know, if I was correct or not! Thanks
Ole Media