php - 如何将 include(php 文件) 放入变量?

标签 php variables include themes

我想要主题或模板像默认一样不受影响,然后其他 php 可以使用它作为他们的主题。但我有一个问题,我无法将主题放入输出打印之前生成的变量中。

示例如下:

主要代码:

$word[0] = "test";
$word[1] = "hello word";
$word[2] = "example";
$word[3] = "wordwordword";
$word[4] = "variable";

while(5)
{
$name = "TEST".$word[$i];
output .= include("theme.php");

$i++;
}
echo "OUTPUT:";
echo "TITLE";
echo $output;

主题.php代码:

hello <?php echo $name; ?>

注意:我知道 while 循环会中断,但这只是示例代码。因为我使用 while(mysql_fetch_array($result))。谢谢

最佳答案

该代码不可能工作,所以我认为它是伪代码。无论如何,通过使用 PHP 中的 ob_* 函数解决了该问题,如下所示。这就是 php 中大多数模板解析器的工作方式:

<?php
$word = array( );
$word[0] = "test";
$word[1] = "hello word";
$word[2] = "example";
$word[3] = "wordwordword";
$word[4] = "variable";

$i = 0;
for( $i = 0, $j = count( $word ); $i < $j; $i ++ ) {
    $name = "TEST" . $word[$i];

    ob_start( );
    include( 'theme.php' );
    $output .= ob_get_clean( );
}

echo $output;
/**
 * Result:
 * 
 * TESTtest
 * TESThello world
 * TESTexample
 * TESTwordwordword
 * TESTvariable
 */

关于php - 如何将 include(php 文件) 放入变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6621796/

相关文章:

javascript - highchart mysql json,带有下拉选择条件的值

php - 尝试获取 MYSQL 变量来打印我的高分

php - 如何在子文件夹页面中显示图像(页面在 PHP 中)

php - 直接消息的 MySql 查询

java - Java 和 PHP 之间的加密不匹配

mysql - bash循环mysql输出多个变量

android - 如何在 strings.xml 中使用 build.gradle 中定义的变量?

c - c 系统的模块化

C++ 程序将包含的 .txt 文件视为代码

javascript - 单选按钮值未存储在数据库中