PHP 包含在变量内部

发布于 2024-10-06 22:13:03 字数 499 浏览 0 评论 0原文

我有一个控制页面输出的函数:

$page = "

{$title}

{$image}
{$desc}
";

我会喜欢在 $page 变量中定义的 html 中包含文件“box.php”。我尝试了这个:

$page = "

{$title}

{$image}
" 。包括(“box.php”); 。 “
{$desc}
";

...但它不起作用。如何将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌路终见情 2024-10-13 22:13:03

来自 php.net

// put this somewhere in your main file, outside the
// current function that contains $page
function get_include_contents($filename) {
    if (is_file($filename)) {
        ob_start();
        include $filename;
        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
    }
    return false;
}

// put this inside your current function
$string = get_include_contents('box.php');
$page  = '<div class="media-title"><h2>{$title}</h2></div>';
$page .= '<div class="media-image">{$image}</div>';
$page .= '<div class="inlinebox">' . $string . '</div>';
$page .= '<div class="media-desc">{$desc}</div>';

from php.net

// put this somewhere in your main file, outside the
// current function that contains $page
function get_include_contents($filename) {
    if (is_file($filename)) {
        ob_start();
        include $filename;
        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
    }
    return false;
}

// put this inside your current function
$string = get_include_contents('box.php');
$page  = '<div class="media-title"><h2>{$title}</h2></div>';
$page .= '<div class="media-image">{$image}</div>';
$page .= '<div class="inlinebox">' . $string . '</div>';
$page .= '<div class="media-desc">{$desc}</div>';
驱逐舰岛风号 2024-10-13 22:13:03

如何将 php include 放入变量中?

# hello.php
<?php
  return "Hello, World!";
?>

# file.php
$var = include('hello.php');
echo $var;

不过我通常会避免这样的事情。

How can I put a php include inside of a variable?

# hello.php
<?php
  return "Hello, World!";
?>

# file.php
$var = include('hello.php');
echo $var;

I would generally avoid such a thing though.

当爱已成负担 2024-10-13 22:13:03

首先,不要在语句内部使用分号。
其次,将 include 语句括在括号中。

$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>";

最后:在“box.php”文件中,您需要执行以下操作:

<?php
ob_start();

// your code goes here

return ob_get_clean();

编辑:有关在函数竞赛之外调用 return 的一些信息:PHP 手册 - 返回

First, don't use a semicolon from inside the statement.
Second, wrap the include statement in parentheses.

$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>";

Finally: In the "box.php" file, you will need to do the following:

<?php
ob_start();

// your code goes here

return ob_get_clean();

EDIT: Some info about calling return outside of the function contest: PHP Manual - Return.

阳光的暖冬 2024-10-13 22:13:03

编辑:

不知道这是否有用,但我认为包含一个文件来获取一段 HTML 并不是一个好的选择。它不可扩展。你可以尝试使用 MVC 之类的东西。您可以要求控制器渲染您想要的内容。

$view = $controler->getElement('box');

$page = "<div class='media-title'><h2>{$title}</h2></div><div class='media-image'>{$image}</div><div class="inlinebox">" . $view . "</div><div class='media-desc'>{$desc}</div>";

尝试解耦你的代码。

我建议您看一下一些 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.

$view = $controler->getElement('box');

$page = "<div class='media-title'><h2>{$title}</h2></div><div class='media-image'>{$image}</div><div class="inlinebox">" . $view . "</div><div class='media-desc'>{$desc}</div>";

Try to decouple your code.

I recommend you to take a look to some MVC Framework, in my opinion, the best one is CakePHP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文