Jump to content

simple question on include.


art15

Recommended Posts

Hi

 

Does it matter the order of include files in php. for eg if I have 5 included files, at the start of the page. say 1.php, 2.php, 3 and so on.. Is it that php will include the stuff from 1.php first as it first in order and try to include the stuff from that file first and then it will go to 2.php? Also does more number of includes slows the processing?

 

Thanks.

Link to comment
Share on other sites

The include process does indeed bring the code into the current script in the order in which it was specified.  Ultimately you can think of your executing script as one giant static script where the full contents of any included scripts replaces the include statement at the exact point that it's included.

 

Yes includes have a cost, but that is not something you need to worry about too much, as without them PHP would probably be close to unmaintainable.

Link to comment
Share on other sites

If the code required in the include in only required inside an IF ELSE statement, I would assume you could include the file INSIDE the statement.

Ex:

if(condition=true){
include(file);
}
else
{
}

 

Since it will not execute the code inside the brackets if the statement proves false, I would assume it will not load the file, hence freeing up some processing.

 

But then again, that's just my theory - I've never tried it.

 

Can anyone prove me right / wrong?

Link to comment
Share on other sites

  • 1 year later...

If the code required in the include in only required inside an IF ELSE statement, I would assume you could include the file INSIDE the statement.

Ex:

if(condition=true){
include(file);
}
else
{
}

 

Since it will not execute the code inside the brackets if the statement proves false, I would assume it will not load the file, hence freeing up some processing.

 

But then again, that's just my theory - I've never tried it.

 

Can anyone prove me right / wrong?

 

Yes, absolutely.

Link to comment
Share on other sites

Programs execute sequentially starting at the first statement and thereafter executing a single statement at a time.  Therefore your code always executes in the order that you have written it in the file.

 

Unless!

 

The exception is when you use code branching features, such as if-else statements or loops.  In the case of if-else statements, some pieces of code will execute and others will not.  In the case of loops, the code will execute sequentially (just as programs always do) starting with the first statement of the loop body, then the next, and the next, until it hits the loop-body end and starts the loop over again.  Eventually the loop itself comes to a finish and code continues executing sequentially one statement at a time as it always does.

 

When PHP loads your program, it reads the entire file one time to parse it.  This is where PHP checks the syntax of your file for errors and determines the locations of loops, if statements, and other constructs.  It only notes their location, it does not execute them.

 

Then your program begins actual execution.  Only lines that need to be executed are actually executed.  If you have blocks of code that are not accessible, even though they're there, they do nothing.

 

Example:

if( false ) {
  include( 'somefile.php' ); // PHP will note that this line of code is here, but it will NEVER execute it
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.