如何使用PHP仅注入PHP页面的某些部分 [英] How to I inject only certain parts of PHP page using PHP
问题描述
我知道我可以使用PHP包含其他文件
I know I can include other files using PHP
<?php include("file.php"); ?>
现在我的问题是如何只注入file.php的某些部分?
Now my question is how do I inject only certain parts of "file.php"?
假设我在file.php中有一些标题代码和一些页脚代码。如何使用正确的分隔部分组织file.php,以及如何仅包含file.php的某些部分( file.php?footer
或类似的东西?)
Lets say I have some header code and some footer code within file.php. How do I organize file.php with the correct separated section, and how do only include certain sections of the file.php (file.php?footer
or something like that?)
推荐答案
最简单(通常)的方法是简单地制作单独的header.php和footer.php文件,并访问它们的位置你需要它们。没有直接支持仅加载文件的部分。
The easiest (and usual) way is to simply make separate header.php and footer.php files, and access them where you need them. There's no direct support for only loading parts of a file.
编辑(响应您对使用单独功能的其他答案的评论) :假设您的file.php如下所示:
Edit (to respond to your comment on the other answer about using separate functions): Let's say your file.php looks like this:
<?php
function header() { ?>
header content goes here
<?php }
function footer() { ?>
footer content goes here
<?php }
?>
然后在您调用它的页面中,您可以使用< ;?php header()?>
和<?php footer()?>
生成您想要的内容。
Then in the page you're calling it into, you can use <?php header() ?>
and <?php footer() ?>
to produce the content where you want it.
这篇关于如何使用PHP仅注入PHP页面的某些部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!