What happens first in PHP, includes or parses?

Let's assume there is no byte cache.

Will there be parsing my_func

before including a and b 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 cached version of the page.
function my_func(){
  //send caching headers
  //header(....);
  exit;
}

      

+2


source to share


3 answers


Why don't you give it a try?

For example, you might have the first file named temp.php

that contains the following:

<?php

$a = my_func();

include 'temp-2.php';

function my_func() {
    die;
}

      

And the second file temp-2.php

that will contain this:

<?php

sleep(5);

      

When you call temp.php

from your web browser, how long does it take for the page to load? Is it almost instantaneous? Or does it take 5 seconds?

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

.

... And after trying: it only takes a moment - this means the second file is not included when there is a stamp or exit in the function.


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

Here's another try: temp.php

still contains the following:



<?php

$a = my_func();

include 'temp-2.php';

function my_func() {
    die;
}

      

But the file temp-2.php

now only contains this:

<?php

,

      

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


If you are calling temp.php

from your problem, that doesn't seem to be the problem: nothing is displayed and there is no parsing error.

Now if you comment out the line " die

" inside the function my_func

and try calling it again temp.php

in your browser, you get:

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

      

Which indicates a Parse error if PHP tries to parse this second file.


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

Hope this answer puts you in a better position, this time :-)

+1


source


Yes, my_func will be called before any actions are taken. They are actually a "runtime" mechanism, not a parse time. You can even enable conditionally by wrapping them in flow control.



+10


source


Basically, the order 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 will change to:

B
C
A

      

0


source







All Articles