0

Consider the following PHP file:

index.php

<!DOCTYPE html>
    <head>
        <title>Welcome to my home page!</title>
    </head>
    <body>
    <?php
    for($i = 0; $i < 2000; ++$i)
    {
        echo $i.'<br>';
    }
    ?>
    </body>
</html>

Is PHP loading into memory only the contents of the echo or the static HTML as well?

Maybe the answer is widely known but I have not found any documentation.

I would love an RTM link btw.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • yes, anything you output (or do) in php will go into memory at some point, even if it's just into an output buffer for a short period until the buffer's flushed. however, anything that's **NOT** within `` php code blocks is treated as plain output by php, and is basically fired "out the door" immediately to whatever php's output environment is.terminal, webserver, blah blah blah – Marc B Jul 30 '15 at 15:55
  • Both the static HTML and the results of any PHP code execution get returned in a buffer to the web server for delivery to the browser – RiggsFolly Jul 30 '15 at 15:57
  • 1
    a webserver's basically like your own home computer, only and most likely much more powerful. If there wasn't any RAM but using a 20mb hard drive, it would spin till infinity. so yeah, a buffer is just that; RAM (GIGO). Here... this Q&A will/should explain it in more detail http://stackoverflow.com/q/6345020/ – Funk Forty Niner Jul 30 '15 at 16:01
  • @MarcB So would the proper chain of events be like this: Apache identifies PHP file **->** Apache invokes the PHP interpreter to read the file **->** PHP interpreter holds the entire file in memory **->** PHP interpreters looks for tags and executes code inside and appends to memory usage **->** PHP interpreter tells Apache that it is done **->** Apache serves HTML, at least in this case, to the browser – MonkeyZeus Jul 30 '15 at 16:08
  • @RiggsFolly See above – MonkeyZeus Jul 30 '15 at 16:08
  • @MonkeyZeus Heading down below now... – Funk Forty Niner Jul 30 '15 at 16:10

2 Answers2

1

The answer is quite simple, to process/find those <? tags the php interpreter needs to read the whole file. So yes php loads the whole file.

The ob (output buffer) functionality of PHP actually allows you to take advantage of this.

See the first example of the on_start() documentation illustrates this

<?php

function callback($buffer)
{
  // replace all the apples with oranges
  return (str_replace("apples", "oranges", $buffer));
}

ob_start("callback");

?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php

ob_end_flush();

?>

http://php.net/manual/en/function.ob-start.php

maazza
  • 7,016
  • 15
  • 63
  • 96
0

PHP's output buffer contains everything.

See: http://php.net/manual/en/language.basic-syntax.phpmode.php

This works as expected, because when the PHP interpreter hits the ?> closing tags, it simply starts outputting whatever it finds (except for an immediately following newline - see instruction separation) until it hits another opening tag unless in the middle of a conditional statement in which case the interpreter will determine the outcome of the conditional before making a decision of what to skip over. See the next example.

So anything outside of a PHP tag is essentially treated as an echo.

Kyle
  • 3,935
  • 2
  • 30
  • 44