Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
您感觉如何<;?php,>;,及<;html>;在包含文件中继承的标记?_Php_Include - Fatal编程技术网

您感觉如何<;?php,>;,及<;html>;在包含文件中继承的标记?

您感觉如何<;?php,>;,及<;html>;在包含文件中继承的标记?,php,include,Php,Include,(如果有更好的方法来表达这个问题,请说出来。) 假设我有一个名为example.php的php文件,看起来像 <?php echo "hello world"; echo "hello world again"; ?> 我想包括另一个名为part2.php的php文件,它包含一些html和php,如下所示: <p>This is some html</p> <?php echo "and this is coming from php"; ?>

(如果有更好的方法来表达这个问题,请说出来。)

假设我有一个名为example.php的php文件,看起来像

<?php
echo "hello world";
echo "hello world again";
?>

我想包括另一个名为part2.php的php文件,它包含一些html和php,如下所示:

<p>This is some html</p>
<?php
echo "and this is coming from php";
?>
<p>more html</p>
这是一些html

更多html

我是否需要在包含文件之前结束php,就像这样

<?php
echo "hello world";
?>
include("part2.php");
<?php
echo "hello world again.";
?>

包括(“part2.php”);
或者我是这样制作example.php的

<?php
echo "hello world";
include("part2.php");
echo "hello again world.";
?>
?>
<p>This is some html</p>
<?php
echo "and this is coming from php";
?>
<p>more html</p>
<?php
<p>This is some html</p>
<?php
echo "and this is coming from php";
?>
<p>more html</p>

然后像这样制作part2.php

<?php
echo "hello world";
include("part2.php");
echo "hello again world.";
?>
?>
<p>This is some html</p>
<?php
echo "and this is coming from php";
?>
<p>more html</p>
<?php
<p>This is some html</p>
<?php
echo "and this is coming from php";
?>
<p>more html</p>
?>
这是一些html

更多html

更多html

一定要做

<?php
echo "hello world";
include("part2.php");
echo "hello again world.";
?>


否则,include将在php标记之外,无法工作。

不要让它过于复杂化。让example.php保持这样

<?php
echo "hello world";
include("part2.php");
echo "hello again world.";
?>

然后在part2.php中,您可以像这样使用它

<p>This is some html</p>
<?php
echo "and this is coming from php";
?>
<p>more html</p>
这是一些html

更多html


PHP必须解析include,因此PHP标记应该在附近

example.php:

<?php
echo "hello world";
include("part2.php");
echo "hello again world.";
?>

示例2.php:

<p>This is some html</p>
<?php
echo "and this is coming from php";
?>
<p>more html</p>
这是一些html

更多html

您可以包含几乎所有内容,因此,如果包含的文件中有php,请在其周围放置标记。
可以在包含的文件中包含另一个文件,以此类推……

如PHP文档中关于函数的描述:

当包含一个文件时,解析将退出PHP模式并转换为HTML 模式,并在 结束。因此,目标文件中应该 作为PHP代码执行必须包含在有效的PHP开始和结束中 标签。

PHP解析器将解释
include
中的代码,就像它是原始include文件的一部分一样,这意味着您的代码将按原样处理:

<?php echo "hello world"; ?>
<p>This is some html</p>
<?php echo "and this is coming from php"; ?>
<p>more html</p>
<?php echo "hello again world."; ?>

这是一些html

更多html


当然,假设您的
include(“part2.php”)
函数被正确地封装在
标记中。

只要阅读文档:,特别是示例2上的简介,您就发现了一些可能的情况。为什么不试试看会发生什么呢?您确实意识到
?>包含(“part2.php”);