php 和 html 的包含顺序和解释
Order of inclusion and interpretation for php and html
我正在尝试根据某些选项在页面上显示不同的内容。
此外,我试图避免对所有 html 输出使用 php echo。
我偶然想到了以下解决方案,现在我对它的实际工作方式感到困惑。
test.php
<?php
function get_content() {
$page = 0;
if($page == 0)
include('page0.php');
else
include('page1.php');
}
?>
<html>
<body>
<?php echo get_content() ?>
</body>
</html>
page0.php
<?php
$link = "http://www.google.ca";
$name = "GOOGLE";
?>
<a href="<?= $link ?>"> <?= $name ?> </a>
page1.php
<?php
$link = "http://www.yahoo.ca";
$name = "YAHOO";
?>
<a href="<?= $link ?>"> <?= $name ?> </a>
似乎 php 解释器在到达下一行时最终会将 html 标记包含到 <?php ?>
块中,但不知何故,这段代码有效,并且输出html有效。
include('page0.php');
谁能解释一下这里到底发生了什么?
When a file is included, parsing drops out of PHP mode and into HTML
mode at the beginning of the target file, and resumes again at the
end. For this reason, any code inside the target file which should be
executed as PHP code must be enclosed within valid PHP start and end
tags.
我正在尝试根据某些选项在页面上显示不同的内容。
此外,我试图避免对所有 html 输出使用 php echo。
我偶然想到了以下解决方案,现在我对它的实际工作方式感到困惑。
test.php
<?php
function get_content() {
$page = 0;
if($page == 0)
include('page0.php');
else
include('page1.php');
}
?>
<html>
<body>
<?php echo get_content() ?>
</body>
</html>
page0.php
<?php
$link = "http://www.google.ca";
$name = "GOOGLE";
?>
<a href="<?= $link ?>"> <?= $name ?> </a>
page1.php
<?php
$link = "http://www.yahoo.ca";
$name = "YAHOO";
?>
<a href="<?= $link ?>"> <?= $name ?> </a>
似乎 php 解释器在到达下一行时最终会将 html 标记包含到 <?php ?>
块中,但不知何故,这段代码有效,并且输出html有效。
include('page0.php');
谁能解释一下这里到底发生了什么?
When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.