gpt4 book ai didi

PHP include 内部变量

转载 作者:可可西里 更新时间:2023-11-01 13:03:52 25 4
gpt4 key购买 nike

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

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

我想在 $page 中定义的 html 中包含一个文件“box.php”多变的。我试过这个:

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

...但是没有用。如何将 php include 放入变量中?

最佳答案

来自 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>';

关于PHP include 内部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4404307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com