Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Includes > PHP Architecture Question

Status
Not open for further replies.

alfalf

Programmer
Mar 6, 2003
155
BA
Hello all.

I have some general questions about INCLUDES in PHP. Perhaps it will sound dumb, but I want to be sure :).

So, PHP (pseudo) code:

if (condition 1)
INCLUDE APPROPRIATE INCLUDE FILE
if (condition 2)
EXECUTE SOME CODE

So, my questions are:

1. Does PHP, while parsing such code, if condition 2 is met, anyway parses include file from condition 1 (without passing it to browser because condition 2 is met)?
2. Does PHP, while parsing such code, if condition 1 is met, anyway parses code from condition 2 (without passing it to browser because condition 1 is met)?

Upper questions are related to bandwidth, page size output and server execution dilema we have here. Thanks to any suggestions.









 
this depends on the structure you use. for example switch structures can create a cascade effect.

for plain old "if" for your pseudo code the INCLUDE bit will only happen if condition 1 is met the EXECUTE bit will only happen of condition 2 is bet and both will only happen if both condition1 and condition2 are met.

if you want EXECUTE NOT to happen even if condition2 is met providing condition1 is met then you change the second if to an elseif.

 
Here is I again.

We found the answer, if anyone interested:

We tested this code:
Code:
<?
$a=1;
if ($a==1){echo 'a is ok';}
if ($a!=1){include blahblah;}
?>

In upper code, there's obvious syntax error include blahblah;, yet, PHP returned this in browser:

a is ok

One can test this easily.

So to conclude what was my question:
PHP will not parse conditioned code, if (in such cases) condition is not met.


The purpose of this TEST was not to make an error, but to find out if PHP parses code under condition didn't met.

This is good to know especially with complex web pages where's a lot of conditioning and including, and one can better predict on how (how fast) will certain web page behave.

If someone knew this, while reading my inquiry, well, good for him / her ;-).

jpadie, thanks.
 
this is probably just semantics but this statement:
PHP will not parse conditioned code, if (in such cases) condition is not met.
is not correct.
php will parse all code on loading the script. if there are any errors then php will fail the script with a parse_error.

php will (of course) not execute any code within a condition if the condition is not met.

to my mind this:
Code:
if ($a==1){echo 'a is ok';}
if ($a!=1){include blahblah;}
would be better expressed as
Code:
if ($a===1){echo 'a is ok';} else {include "blahblah.txt";}
 
Thank You, jpadie.

Your statement is 'true' UNDER lots of circumstances.

To add:
PHP will only parse the syntax of all code, but will NOT include file in parse (neither check its syntax, PHP will not even check against its existance!) if condition for inclusement is not met.

That is better description of our results.

Anyhow, if ... elseif ... else is known function, I don't want to arrouse that question here.

Anyway, thx.
 
And that is important to know, since we struggle (because of loads of javascript) with download time execution.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top