开发者

What renders the HTML?

In PHP you can write html along with PHP, like:

<html>
    <head>
        <?php echo "HTML Title from PHP "; ?>
    </head>
    <body>开发者_开发技巧;
        <h1>hello world!</h1>
    </body>
</html>

I would like to know what is this, and how it is working, and in what was written, and how deep do I need to go to understand it?

Nowadays I hear about template engines, but there are lots of template engines for PHP too, so it sounds stupid(for me) to write a template engine on a template engine also as I understood, a template engine parses the whole file just to replace the commands, then it starts to output the file contents too, so it doesn't feels to me that it would be the part of the system, thus loosing performance. ( Maybe I'm totally wrong but seriously this is what I feel when I hear it :P )

EDIT

Guys, when I say what renders HTML I mean what renders HTML in PHP? In a node.js file you can't write any HTML because nothing is processing it.


Trying to avoid too much information, but here is the full cycle of PHP when used with Apache on Linux.

Prerequisities

A common server setup is called the LAMP stack which stands for Linux, Apache, Mysql, and PHP. Generally you can find dozens of ready to use LAMP stack setups along with guides for using them so in context of your question, all you need to focus on is Apache and PHP.

Stage 1 - Delegation

When a web browser contacts a web server running Apache with PHP, the first step is for Apache to find the desired content. Say you go to www.mywebsite.com/hello.php, Apache is going to see you're looking for a file called hello.php. At this point, because of the suffix (.php) , Apache knows that this file needs to be interpreted by PHP so it delegates processing to the PHP interpreter.

Stage 2 - Setup

The hand over from Apache to PHP includes a slew of headers that tells PHP the following: what kind of transaction is being processed ( GET/ POST/ PUT/ DELETE ), the IP address of the incoming request, the browser's user agent ( Firefox, MSIE , IPhone, etc ), if there are cookies. More importantly, Apache hands to PHP the path to hello.php file on the server.

Stage 3 - Processing

Depending on configuration, PHP might need to do some basic house keeping ( setting itself up ) but under ideal conditions its ready to go and opens hello.php Part of PHP is a module called a lexer which looks at hello.php and figures out how to deal with the file. With the example provided a very simple example might look like:

  1. T_STRING = "<html>\n\t<head>\n\t"
  2. T_OPEN_TAG;
  3. T_ECHO;
  4. T_STRING = "HTML Title from PHP ";
  5. ;
  6. T_CLOSE_TAG;
  7. T_STRING = "\t</head>\n\t<body>\n\t<h1>hello world!</h1>\n\t</body>\n</html>"

Note, I've made up most of the T_ codes, but they're pretty close to what is real.

Line 1 - PHP knows it's outside of what called scope, so it immediately passes this entire string to Apache, the webserver. Apache will most likely then pass this entire string onto the web browser.

Line 2. - T_OPEN_TAG tells PHP it's entering into the PHP scope and waits it's first instruction.

Line 3 - T_ECHO tells PHP it's going to make an echo statement, so it's rules then kick in to look for an expression or string to output.

Line 4 - As luck would have it, the next token is a string, so PHP now knows it's going to echo "HTML Title from PHP "

Line 5 - The ; tells PHP that the echo statement is complete and more importantly this is syntactically correct... so PHP hands the string "HTML Title from PHP" to Apache which passes this onot the browser.

Line 6 - The Close ?> tag tells PHP that it is leaving the PHP language scope, so it goes back to a much simpler set of rules

Line 7 - Like line 1, the entire string is passed to Apache to be passed to the web browser

At this point, PHP reaches what's called EOF or end of file and knows it's done processing the file hello.php. It does cleanup work and then tells Apache it is done.

Finalization

At this point the request has been mostly fulfilled, Apache will most likely hang up on the web browser, sending notification that all content is complete.

Let me know if you have any questions or need any pointers on where to go next. Also note there is a LOT of details left out of here for brevity/sanity sake, but for just understanding how birds eye view of PHP's relationship to the web browser & web server, this should be enough to get started.

Demonstration script

$test = 'Hello world <' . '?' . 'php echo \'this is in scope\'; ?' . '> and we\'re done';

 $tokens = token_get_all($test);

 print_r($tokens);

The output will be real world token string PHP generates. Each token can be either a string or a three element tuple/numeric array where index 0 == the Token ID, index 1 == raw string, and I can't for the life of me remember what the third element is. Use token_name if your curious what each token's name is.


First off, PHP IS a template engine, when you use it like in your example.

You really don't need anything more, if you want to keep it simple.


anything inside the <? ?> braces is php code - it's executed on the serverside, and then it outputs the plain HTML to the client, at which point the client's web browser renders it.

Have a look at this diagram.


Your browser is only ever the thing that renders HTML. Whether its written via PHP, javascript or asp, in the end, the thing that works out what the text means (as html is only text) is your browser.


the rendered final product is handled by your web browser. (FF is written in C/C++)

before transmission, apache runs all php scripts thru PHP (preprocessor) written in C

if you use a PHP template engine (eg: Smarty) that is written in PHP itself

if you're really concerned with performance, then switch to another language. PHP template engines are not about conserving CPU cycles, or even speed, instead they boost productivity and maintainability, furthermore they empower front-end developers with the ability to maintain more complex templates without knowing PHP, which increases the value of their work, while free developers to do more serious coding.


Well, you need to understand the history of web development to give you an "answer" to your question.

In the 199x when Web development started a lot of people uses Perl. In these days you often have written code like this:

print "<html>";
print "    <head>";
print "    <title>", get_site_name(), "</title>";
print "    </head>";
print "<body>Hello, World!</body>";
print "</html>";

As you can see in this example, in these days you often end up writing a lot of HTML, and just include some little dynamic stuff. Like a counter, a clock that shows time, or other little things. In these days PHP was born to make this process a lot easier. Instead of writting Code that prints a lot of HTML, PHP was/is a templateing Engine that allows you to write HTML and just include your code. In these days you probably end up writting 90% HTML and 10% of you logic. In these days, that was oft everything what you needed and it was a lot easier this way.

But time changed. Today web development got a lot complexer as in the earlier days. Today you probably write 90% logic and just 10% HTML. Today nearly everything gets automatically generated, and comes from a database. Because of the complexion that raised, today you often write your PHP Application like

<?php
    echo "<html>";
    echo "<head>";
echo "...";
echo "</html>";
?>

Well as you can see, evolutions goes backwards. And is not a big difference as in the days before. PHP programs most ended in starting the php Tag and generating everything and echo out everything what is needed like in the old days with Perl.

But, that is still not enoug, you end where you started. Because today you have a lot more logic to write in your application, it is a good idea to seperate your application into many peaces. You seperate your application intp logic and how you should display your data.

The logic itself get written in PHP, but to display the data people tend to use other templating engines, that are much more modern today and fits the needs better.

Yes, it seems a little awkward to have a Templating Engine that raised to a general purpose language, where Templating Engine is written in, but that is how it is.

The question how HTML get rendered, well you have a "php" interpreter often directly embedded in Apache (mod_php) that do all the stuff, but for this description there already exists better answers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜