1

If I place PHP code prior to the <html> tag I assume it will be executed prior to the page load. But if I place the same code inside the <body> tag, will the PHP wait for the page load to finish first?

KingsInnerSoul
  • 1,373
  • 4
  • 20
  • 49
  • 4
    No. PHP code is ***always*** executed prior to the page load. PHP evaluates the entire code in the script, process it, and sends the HTML output. The browser then displays it. – Amal Murali May 09 '14 at 12:32
  • 1
    PHP is executed on the server, so it has little to do with the client loading the page. – CBroe May 09 '14 at 12:32

2 Answers2

5

PHP is executed before the page is sent to the client, it doesn't matter where you put your content, PHP will ALWAYS be executed first.

scragar
  • 6,764
  • 28
  • 36
1

The PHP runs, outputting anything outside the <?php ?> tags as it goes.

The output might be buffered and then sent in one go once the script has finished.

The output might be sent bit by bit as the script outputs it.

(Which depends on how the script is written).

If you have <?php foo(); ?> just after <body>, then it will send the body start tag to output, then execute foo, then output whatever follows.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    how about a case where I omit the opening `` tag in the HTML and add it to the PHP `"; ?>` will that kinda force it to be executed in some other order? – KingsInnerSoul May 09 '14 at 12:37
  • 2
    No. The order will be exactly the same. Switching between output mode and execution mode has no impact on execution order, ever. – Quentin May 09 '14 at 12:40