If include () is conditional, will PHP include the file even if the condition is not met?

This has been in my opinion for some time now and I decided that I should find an answer from the experts. I want to know if this is a bad programming way to funnel all PHP requests through one file. I am working on a website and am not sure if it will scale with growth because I am not 100% sure how PHP handles the function include()

.

To better explain how I built my quasi-framework, this is a snippet of my root file .htaccess

:

# > Standard Settings
RewriteEngine On

# Ignore all media requests
RewriteRule ^media/ - [L]

# Funnel all requests into model
RewriteRule ^(.*)$ _model.php [QSA]

      

So, everything but the content in the media directory gets passed into this single script.

Internally _model.php

I have all my input sanitation, user authentication, session data being fetched from the database, any global variables (commonly used variables like $ longTime, $ longIP, etc.). Requests are routed by interpreting a variable $_SERVER["REQUEST_URI"]

.

Essentially, I have an operator switch()

that selects which module for include()

. I don't understand: when PHP does, will it execute each include()

regardless of whether the case directive is true?

I am concerned that after time I will have a lot of these modules - and if PHP turns on all modules at runtime, it will end up taking up too much computational power and RAM ...

-

Edit:
I'm just asking if PHP will "read" all those files it could potentially include. I know that it shouldn't actually execute the code. If one of mine include()

is a 2GB file that takes a long time to process, will PHP always read that file before executing?

- Edit:
I found another similar question (I searched a lot before posting this one) PHP include / require behavior inside a conditional

I think I can close this.

+3


source to share


2 answers


No, PHP will perform the include the moment the code snippet is reached.

This is very important because you can link the file directly to the code directly. For example.

File1:

<?php echo "Foo"; ?>

      



File2:

<?php
  echo "Before";
  include("File1");
  echo "After";
?>

      

Sometimes your PHP processor doesn't even know at compiletime which file to include. Imagine something like include("File".mt_rand(1,10));

. PHP will not know the name of the file to be included until it reaches the include statement.

+2


source


will PHP include the file even if the condition is not met?

No, operators are include

both require

interpreted and evaluated in the same way as other PHP instructions. PHP does not scan the script for inclusion before executing.

This can be verified with a simple test:



if(false)
{
    include('does_not_exist.php');
}

      

The above does not generate errors or warnings. If the inclusion was read before execution, you will see a warning like:

Warning: include (do_not_exist.php): failed to open stream: no such file or directory ...

Warning: include (): Could not open "do_not_exist.php" to include ...

+2


source