我想要主题或模板像默认一样不受影响,然后其他 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/