简体   繁体   English

php包含和关闭标记

[英]php include and closing tag

Saw a thread about omitting the closing ?> in PHP scripts and got me wondering. 在PHP脚本中看到一个关于省略关闭?>的线程,让我感到疑惑。

Take this code: 拿这个代码:

foo.php foo.php

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

bar.php bar.php

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

If you create these two scripts and run them, php outputs: 如果你创建这两个脚本并运行它们,php输出:

This is foo.php
This is bar.php

(new line added for artistic license before anyone points that out) (在任何人指出之前添加了艺术许可的新行)

So, how come: baz.php 那么,为什么: 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. 导致可预测的语法错误unexpected '<' ,当“include”就是这样 - 或者更确切地说,我对include的理解是PHP只是在那时转储文件,好像它一直存在。

Does PHP check for opening tags and ignore future ones if the file is included? 如果文件包含在内,PHP是否检查开始标记并忽略未来标记? 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. 这不是一个重要的问题,但更好地理解PHP。

If you include a file, PHP internally switches from parsing to literal mode (ie what it normally does on a closing tag. That's why this works: 如果你包含一个文件,PHP会在内部从解析转换为文字模式(即通常在结束标记上执行的操作。这就是为什么这样做的原因:

<?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. 您可以省略关闭?>因为在包含文件的末尾,PHP切换回解析包含文件,关注它当前所处的模式。

Personal speculation: 个人猜测:

When you write include('bar.php'); 当你写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. ,解析器读取bar.php的内容并将其插入到foo.php ,当读取它时可能会 foo.php起始的<?php因为它识别bar.php所有内容都是PHP代码,因此结果不会发生错误。错误。

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. 将PHP视为实际上不包括第一个中的其他页面可能会有所帮助,因为在PHP内部不会在内部创建包含所有包含页面的单个文件,然后解析它。

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. 它的作用是解析第一个文件,查找include然后停止解析第一个文件并开始解析包含的文件。 When the included file is done it resumes parsing the original file where it left off. 当包含的文件完成后,它将继续解析它停止的原始文件。 (This is quite a bit simplified though.) (虽然这有点简化。)

I somewhere read about omitting both opening and closing php tags. 我在某处读到了省略开启和关闭php标签。

The problem when I do this is that my editor doesn't format the files as php files but as regular text files. 我这样做的问题是我的编辑器不将文件格式化为php文件,而是将其格式化为常规文本文件。

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. 我不知道使用标签是否是折旧行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM