PHP 包含在变量内部
我有一个控制页面输出的函数:
$page = "
{$title}
我会喜欢在 $page
变量中定义的 html 中包含文件“box.php”。我尝试了这个:
$page = "
{$title}
...但它不起作用。如何将 php include 放入变量中?
I have a function that is controlling the output of my page:
$page = "<div class='media-title'><h2>{$title}</h2></div><div class='media-image'>{$image}</div><div class='media-desc'>{$desc}</div>";
I would like to include a file "box.php" inside that html that is defined in the $page
variable. I tried this:
$page = "<div class='media-title'><h2>{$title}</h2></div><div class='media-image'>{$image}</div><div class="inlinebox">" . include("box.php"); . "</div><div class='media-desc'>{$desc}</div>";
... but it didn't work. How can I put a php include inside of a variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自 php.net
from php.net
不过我通常会避免这样的事情。
I would generally avoid such a thing though.
首先,不要在语句内部使用分号。
其次,将 include 语句括在括号中。
最后:在“box.php”文件中,您需要执行以下操作:
编辑:有关在函数竞赛之外调用 return 的一些信息:PHP 手册 - 返回。
First, don't use a semicolon from inside the statement.
Second, wrap the include statement in parentheses.
Finally: In the "box.php" file, you will need to do the following:
EDIT: Some info about calling return outside of the function contest: PHP Manual - Return.
编辑:
不知道这是否有用,但我认为包含一个文件来获取一段 HTML 并不是一个好的选择。它不可扩展。你可以尝试使用 MVC 之类的东西。您可以要求控制器渲染您想要的内容。
尝试解耦你的代码。
我建议您看一下一些 MVC 框架,在我看来,最好的一个是 CakePHP。
Edit:
Don't know if this is useful, but i think that including a file to get a piece of HTML, is not a good option. It's not scalable. You could try with something like MVC. You could ask your controller to renderize the content of what you want.
Try to decouple your code.
I recommend you to take a look to some MVC Framework, in my opinion, the best one is CakePHP.