views:

151

answers:

3

Assume no byte cache.
Will the my_func be parsed before a & b are included, or after?

$x=my_func();

//a.php and b.php are **very** heavy scripts
include ('a.php');
include ('b.php');


//my_func makes the browser use it's cached version of the page.
function my_func(){
  //send caching headers
  //header(....);
  exit;
}
+10  A: 

Yes, my_func will be called before the includes are executed. They're effectively a "run time" mechanism, rather than parse time. You can even include conditionally, by wrapping them in flow control.

Adam Wright
SO, just to make it cleared for me. Includes happens in the "running phase" and **not** in the parsing phase?
Itay Moav
Yes - includes are included at runtime.
Adam Wright
A: 

Basically the ordering will be correct. Consider:

a.php:

<?php echo "A\n"; ?>

b.php:

<?php echo "B\n"; ?>

c.php:

<?php
my_func();

include 'a.php';
include 'b.php';

function my_func() {
  echo "C\n";
}
?>

The output will be:

C
A
B

But change c.php to:

<?php
my_func();

include 'a.php';

function my_func() {
  include 'b.php';
  echo "C\n";
}
?>

and the output changes to:

B
C
A
cletus
what if the function my_func is in a.php or b.php?
andho
Answer to my own questionFatal error: Call to undefined function my_func() in D:\Program Files\xampp5\htdocs\test\test2.php on line 2
andho
If you're getting a fatal error you've probably got a typo somewhere because the above works fine.
cletus
+1  A: 

Why don't you try ?

For instance, you could have a first file, called temp.php, that contains this :

<?php

$a = my_func();

include 'temp-2.php';

function my_func() {
    die;
}

And the second file, temp-2.php, that would contain this :

<?php

sleep(5);

When you call temp.php from your web-browser, how long does it take for the page to load ? It is almost instantaneous ? Or does it take 5 seconds ?

In the first case, the function is called before temp-2.php is included.

... And, after trying : it does take only an instant -- which means the second file is not included, when there is a die or exit in the function.


EDIT after the comment : oh, sorry, I didn't really understand the question, I suppose :-(

Here is another try : the temp.php still contains this :

<?php

$a = my_func();

include 'temp-2.php';

function my_func() {
    die;
}

But the temp-2.php file now contains only that :

<?php

,

Which, yes, will get you a parse error if PHP tries to parse this file.


If you call temp.php from your problem, it doesn't seem to be any problem at all : nothing is displayed, and there is no parse error.

Now, if you comment the "die" line inside the my_func function, and try calling temp.php again in your browser, you get :

Parse error: syntax error, unexpected ',' in /home/squale/developpement/tests/temp/temp-2.php on line 3

Which indicates there is a Parse error if PHP tries to parse that second file.


So, the first time, the function has been called before PHP actually tried to parse the second file.

Hope this answer you question better, this time :-)

Pascal MARTIN
Itay Moav
@Itay : I've just edited my answer with a second exemple, which, I hope, answers your question better.
Pascal MARTIN
Although I got a straight answer by Adam Wright, I like yours better cause of the proof. Thanks.
Itay Moav
You're welcome ! Have fun :-)
Pascal MARTIN