Home » PHP » PHP conditional include statement parsing

PHP conditional include statement parsing

I’m writing some PHP and using include statements as is pretty common.  When using conditional includes in my code I wasn’t quite sure whether the content of the included file is included regardless and then ignored or is only included conditionally (which of course would be the better option from a memory consumption perspective and so on), so I set out to find some info about PHP conditional includes, but surprisingly it wasn’t so easy to find.

I’ve finally managed to come across this PHP Include article that confirm what I was hoping for and also something that I didn’t know.  Apparently when an include is conditional, the content won’t be included if the condition evaluates to false, while if you use a require the content will be included but will not execute.  The last statement seems a little weird since I would expect that if logic is used to work out the source for the require statement that source might not exectute because it’s inside the conditional block, but that may be the exception.

Anways, it’s good to know that files which are conditionally included won’t get parsed.

Categories: PHP Tags:
  1. April 9th, 2012 at 23:29 | #1

    your information is somewhat true. It will only include require files even if the conditional is false on php versions prior to 4.something I don’t recall the exact version number it was changed. The primary difference between include, and require is that include will throw a warning if file was not found and require will throw a fatal error.

    Here is a link for more information:

  1. No trackbacks yet.

four − = 3