php 包含和结束标签

发布于 2024-10-04 11:04:09 字数 754 浏览 0 评论 0原文

看到一个关于在 PHP 脚本中省略结束 ?> 的帖子,这让我感到好奇。

采取以下代码:

foo.php

<?php
echo 'This is foo.php';
include('bar.php');

bar.php

<?php   
echo 'This is bar.php';

如果您创建这两个脚本并运行它们,php 输出:(

This is foo.php
This is bar.php

在任何人指出之前添加新行以获取艺术许可)

那么,怎么会: baz.php

<?php
echo 'This is foo.php';

<?php
echo 'This is bar.php';

会导致可预测的语法错误 意外的 '<',当“include”正是这样做时 - 或者更确切地说,我对 include 的理解是 PHP 只是转储就好像它一直在那里一样。

如果包含该文件,PHP 是否会检查开始标签并忽略未来的标签?当一个脚本中有两组标签时,为什么不这样做呢?

感谢您的任何澄清。不完全是一个重要的问题,但如果能多了解一点 PHP 就好了。

Saw a thread about omitting the closing ?> in PHP scripts and got me wondering.

Take this code:

foo.php

<?php
echo 'This is foo.php';
include('bar.php');

bar.php

<?php   
echo 'This is bar.php';

If you create these two scripts and run them, php outputs:

This is foo.php
This is bar.php

(new line added for artistic license before anyone points that out)

So, how come:
baz.php

<?php
echo 'This is foo.php';

<?php
echo 'This is bar.php';

results in a predictable syntax error unexpected '<', when "include" does just that - or rather, my understanding of include is that PHP just dumps the file at that point as if it had always been there.

Does PHP check for opening tags and ignore future ones if the file is included? Why not do this when there are two sets of tags in one script?

Thanks for any clarification. Not exactly an important issue but would be nice to understand PHP a little more.

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

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

发布评论

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

评论(4

感性不性感 2024-10-11 11:04:09

如果你包含一个文件,PHP会在内部从解析模式切换到文字模式(即它通常在结束标签上执行的操作。这就是为什么它有效:

<?php
include 'foo.php';
?>

//foo.php
<?php
echo 'yo';
?>

即使在内联时它会变成

<?php
<?php
echo 'yo';
?>
?>

因为内部它会转换成这样的东西(出于说明目的,实际上,它可能实际上并没有合并文件的内容,它只是在文件之间跳转)

<?php
?>
<?php
echo 'yo';
?>
<?php
?>

您可以省略结尾 ?> 因为在包含文件的末尾,PHP 会切换回解析包含文件,无论是什么当前所处的模式。

If you include a file, PHP internally switches from parsing to literal mode (i.e. what it normally does on a closing tag. That's why this works:

<?php
include 'foo.php';
?>

//foo.php
<?php
echo 'yo';
?>

Even though when inlined it would become

<?php
<?php
echo 'yo';
?>
?>

Because interally it's transformed into something like this (for illustrative purposes, in reality it probably doesn't actually merge the contents of the files, it just jumps between them)

<?php
?>
<?php
echo 'yo';
?>
<?php
?>

You can omit the closing ?> because at the end of the include file, PHP switches back to parsing the including file, regardles of what mode it's currently in.

骑趴 2024-10-11 11:04:09

个人猜测:

当你编写 include('bar.php'); 时,解析器会读取 bar.php 的内容并将其插入 foo.php< /code>,在读取它时可能会去掉开头的,因为它识别出bar.php的所有内容都是PHP代码,因此结果不会产生错误。

Personal speculation:

When you write include('bar.php');, the parser reads the content fo bar.php and inserts it into foo.php, when reading it probably strips the starting <?php since it recognise all the content of bar.php is PHP code, hence the result does not yelds to an error.

﹂绝世的画 2024-10-11 11:04:09

将其视为 PHP 实际上并未在第一个页面中包含其他页面可能会有所帮助,因为 PHP 不会在内部创建包含所有包含页面的单个文件,然后对其进行解析。

它的作用是解析第一个文件,找到 include 然后停止解析第一个文件并开始解析包含的文件。当包含的文件完成后,它会从中断处继续解析原始文件。 (不过这有点简化了。)

It might help to think of it as PHP not actually including the other page inside the first one, as in PHP doesn't internally create a single file with all included pages inside and then parse that.

What it does is that it parses the first file, finds include and then stops parsing the first file and starts parsing the included file. When the included file is done it resumes parsing the original file where it left off. (This is quite a bit simplified though.)

牵你的手,一向走下去 2024-10-11 11:04:09

我在某处读到关于省略开始和结束 php 标签的内容。

当我这样做时,问题是我的编辑器不会将文件格式化为 php 文件,而是将其格式化为常规文本文件。

到目前为止,我对任何包含标签的包含文件都没有遇到过问题。我不知道使用标签是否是贬值行为。

I somewhere read about omitting both opening and closing php tags.

The problem when I do this is that my editor doesn't format the files as php files but as regular text files.

up till now I never had a problem with any include file that has the tags. I don't know if it is depreciated behavior to use the tags or not.

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