什么呈现HTML? [英] What renders the HTML?

查看:135
本文介绍了什么呈现HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,您可以使用 PHP 来编写html,如下所示:

 < html> 
< head>
<?php echoHTML标题来自PHP; ?>
< / head>
< body>
< h1> hello world!< / h1>
< / body>
< / html>

我想知道这是什么,它是如何工作的以及是写了,我需要了解它有多深?



现在我听说模板引擎,但有很多 PHP的模板引擎,所以在模板引擎上编写模板引擎听起来很愚蠢(据我了解,模板引擎会分析整个文件只是为了替换命令,那么它也开始输出文件内容,所以它不会感觉它是系统的一部分,从而失去了性能。 (也许我完全错了,但认真这就是我听到它时的感受:P)

编辑



伙计们,当我说什么呈现HTML我是指在PHP中呈现HTML的东西?在一个node.js文件中,你不能写任何HTML,因为没有处理它。

,但这里是在Linux上与Apache一起使用时PHP的完整周期。



先决条件



通用服务器setup被称为LAMP堆栈,代表Linux,Apache,Mysql和PHP。一般来说,您可以找到数十个随时可用的LAMP堆栈设置,以及在问题的上下文中使用它们的指南,您需要关注的只有Apache和PHP。



第1阶段 - 委托



当Web浏览器联系使用PHP运行Apache的Web服务器时,第一步是让Apache找到所需的内容。说你去www.mywebsite.com/hello.php,Apache会看到你正在寻找一个名为hello.php的文件。此时,由于后缀(.php),Apache知道该文件需要由PHP解释,因此它将处理委托给PHP解释器。



第2阶段 - 安装



从Apache到PHP的交付包括一大堆头文件,告诉PHP以下内容:正在处理什么类型的事务(GET / POST / PUT / DELETE ),传入请求的IP地址,浏览器的用户代理(Firefox,MSIE,IPhone等),如果有cookie的话。更重要的是,Apache向PHP提供服务器上hello.php文件的路径。



第三阶段 - 处理



根据配置的不同,PHP可能需要做一些基本的管理工作(设置自己),但是在理想条件下准备好并打开hello.php PHP的一部分是一个名为lexer的模块,它查看hello.php和数字了解如何处理文件。通过示例提供的一个非常简单的示例可能如下所示:


  1. T_STRING =< html> \\\< head> \\\
    \t

  2. T_OPEN_TAG;

  3. T_ECHO;

  4. T_STRING =HTML标题来自PHP;

  5. ;
  6. T_CLOSE_TAG;

  7. T_STRING =\t \\\
    \t< body> \\\
    \t< h1> hello world!< / h1> \\\
    \t< / body> \\\
    < / html>

请注意,我已经编写了大部分T_代码,但它们非常接近真实。第1行 - PHP知道它不在所谓的范围内,所以它立即将这整个字符串传递给Apache,即网络服务器。 Apache很可能会将整个字符串传递到Web浏览器中。



第2行 - T_OPEN_TAG告诉PHP它进入PHP范围并等待它的第一条指令。第3行 - T_ECHO告诉PHP它将创建一个echo语句,所以它是规则然后开始寻找表达式或字符串输出。

$ b

第4行 - 幸运的是,下一个标记是一个字符串,所以PHP现在知道它会回显HTML标题来自PHP。



第5行 - 告诉PHP,echo语句是完整的,更重要的是,这在语法上是正确的...所以PHP将字符串HTML Title from PHP传递给Apache,并通过浏览器传递这些信息。



第6行 - Close?>标记告诉PHP它将离开PHP语言范围,因此它返回到一套更简单的规则集。

第7行第7行 - 和第1行一样,整个字符串被传递给Apache以传递给Web浏览器。



此时,PHP达到了所谓的EOF或文件结束并知道它是完成处理文件hello.php。它完成清理工作,然后告诉Apache它已完成。



敲定



此时请求已大多数情况下,Apache很可能会挂在网络浏览器上,发送所有内容已完成的通知。



让我知道你是否有任何疑问或需要任何指示下一步该去哪里。另外请注意,为了简洁/清晰起见,这里留有很多细节,但仅仅了解鸟儿如何看待PHP与网络浏览器和浏览器的关系。 Web服务器,这应该足以开始。



演示脚本



  $ test ='Hello world<'。 '?'。 'php echo \'这是在范围内';'; ?'。 >我们完成了'; 

$ tokens = token_get_all($ test);

print_r($ tokens);

输出将是PHP生成的真实世界标记字符串。每个标记可以是一个字符串,也可以是一个三元组元组/数组,其中索引0 ==标记ID,索引1 ==原始字符串,而且我不能记住第三个元素是什么。如果您好奇每个令牌的名称是什么,请使用 token_name


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.

这篇关于什么呈现HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆