什么呈现 HTML?

发布于 2024-11-12 06:24:22 字数 829 浏览 2 评论 0 原文

在 PHP 中,您可以与 PHP 一起编写 html,例如:

<html>
    <head>
        <?php echo "HTML Title from PHP "; ?>
    </head>
    <body>
        <h1>hello world!</h1>
    </body>
</html>

我想知道什么是这样的吗?它是如何工作的,内容是怎样写的,我需要多深入才能理解它?

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

编辑

伙计们,当我说什么呈现 HTML 时,我的意思是什么在 PHP 中呈现 HTML?在 Node.js 文件中,您无法编写任何 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

他夏了夏天 2024-11-19 06:24:22

尽量避免提供太多信息,但这里是 PHP 在 Linux 上与 Apache 一起使用时的完整周期。

先决条件

常见的服务器设置称为 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 将服务器上 hello.php 文件的路径交给 PHP。

第 3 阶段 - 处理

根据配置,PHP 可能需要做一些基本的内务管理(设置自身),但在理想条件下,它已准备好并打开 hello.php PHP 的一部分是一个称为词法分析器的模块,它查看 hello.php并找出如何处理该文件。根据提供的示例,一个非常简单的示例可能如下所示:

  1. T_STRING = "\n\t\n\t"
  2. T_OPEN_TAG;
  3. T_ECHO;
  4. T_STRING = "来自 PHP 的 HTML 标题";
  5. ;
  6. T_CLOSE_TAG;
  7. T_STRING = "\t\n\t\n\t

    你好世界!

    \n\t\n"

请注意,我已经编写了大部分 T_ 代码,但它们非常接近真实的代码。

第 1 行 - PHP 知道它超出了所谓的范围,因此它立即将整个字符串传递给 Web 服务器 Apache。 Apache 很可能会将整个字符串传递到 Web 浏览器。

第 2 行 - T_OPEN_TAG 告诉 PHP 它正在进入 PHP 作用域并等待它的第一条指令。

第 3 行 - T_ECHO 告诉 PHP 将生成一个 echo 语句,因此它的规则随后会启动以查找要输出的表达式或字符串。

第 4 行 - 幸运的是,下一个标记是一个字符串,因此 PHP 现在知道它将回显“来自 PHP 的 HTML 标题”

第 5 行 - ;告诉 PHP echo 语句已完成,更重要的是,这在语法上是正确的...因此 PHP 将字符串“HTML Title from PHP”传递给 Apache,Apache 将其传递到浏览器。

第 6 行 - 收盘价?>标签告诉 PHP 它正在离开 PHP 语言范围,因此它返回到一组简单得多的规则

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

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

完成

此时请求已基本完成,Apache 很可能会挂起 Web 浏览器,发送所有内容已完成的通知。

如果您有任何疑问或需要下一步操作的指导,请告诉我。另请注意,为了简洁/理智起见,此处省略了很多细节,但只是为了了解 PHP 与 Web 浏览器和 Web 浏览器之间的关系如何鸟瞰。 Web 服务器,这应该足以开始。

演示脚本

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

 $tokens = token_get_all($test);

 print_r($tokens);

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

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.

三岁铭 2024-11-19 06:24:22

首先,PHP 是一个模板引擎,当您像示例中那样使用它时。

如果你想保持简单,你真的不需要更多的东西。

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.

佞臣 2024-11-19 06:24:22

大括号是 php 代码 - 它在服务器端执行,然后将纯 HTML 输出到客户端,此时客户端的 Web 浏览器将呈现它。

看看这个图表

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.

调妓 2024-11-19 06:24:22

您的浏览器只是呈现 HTML 的东西。无论是通过 PHP、javascript 还是 asp 编写,最终,决定文本含义的东西(因为 html 只是文本)是您的浏览器。

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.

内心旳酸楚 2024-11-19 06:24:22

渲染的最终产品由您的网络浏览器处理。 (FF是用C/C++编写的)

在传输之前,

如果您使用PHP模板引擎(例如: Smarty) 是用 PHP 本身编写的,

如果您真的关心性能,那么请切换到另一种语言。 PHP 模板引擎并不是为了节省 CPU 周期,甚至是速度,而是提高生产力和可维护性,此外,它们使前端开发人员能够在不了解 PHP 的情况下维护更复杂的模板,这增加了他们的工作价值,同时免费开发人员进行更认真的编码。

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.

痴梦一场 2024-11-19 06:24:22

好吧,你需要了解 Web 开发的历史
为您的问题提供“答案”。

在 199 年代,当 Web 开发开始时,很多人使用
珀尔。这些天你经常写出这样的代码:

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

正如你在这个例子中看到的,这些天你经常结束
编写大量 HTML,只包含一些动态的小东西。
像计数器、显示时间的时钟或其他小东西
事物。如今PHP的诞生使这个过程变得很多
更轻松。与其编写打印大量 HTML 的代码,
PHP 曾经是/现在是一个模板引擎,允许您编写
HTML 并只包含您的代码。这些天你可能
最终编写 90% 的 HTML 和 10% 的逻辑。在这些日子里,
这通常就是你所需要的一切,而且要容易得多
这边走。

但时代变了。如今 Web 开发变得更加复杂
和早些时候一样。今天你可能写了 90% 的逻辑,只是
10% HTML。今天几乎一切都得到了
自动生成,来自数据库。由于
脸色扬起,今天你经常写你的PHP

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

像您所看到的那样,进化是倒退的 并且不是
和前几天有很大的不同。 PHP程序
大多数以启动 php 标签并生成所有内容结束
并像过去那样回响所需要的一切
与 Perl 一起。

但是,这还不够,你从开始的地方结束。因为
今天你有更多的逻辑要在你的应用程序中编写,
将您的应用程序分成许多部分是个好主意。
您将应用程序 intp 逻辑与显示方式分开
您的数据。

逻辑本身是用 PHP 编写的,但要显示
数据人员倾向于使用其他模板引擎,这些引擎有很多
更现代,更能满足需求。

是的,有模板引擎似乎有点尴尬
提升为通用语言,其中模板化
Engine 是写进去的,但就是这样。

关于 HTML 如何渲染的问题,你有一个“php”解释器,通常直接嵌入到 Apache (mod_php) 中来完成所有的工作,但对于这个描述,已经存在更好的答案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文