Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将include(phpfile)放入变量?_Php_Variables_Include_Themes - Fatal编程技术网

如何将include(phpfile)放入变量?

如何将include(phpfile)放入变量?,php,variables,include,themes,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("the

我想像默认的主题或模板一样不动,然后其他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;
theme.php代码:

hello <?php echo $name; ?>
你好
注意:我知道while循环将中断,但这只是示例代码。因为我使用while(mysql\u fetch\u数组($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
 */

而(5)
?这会结束吗(例如,
theme.php
中是否有
break
)?不,这只是一个例子。。。唯一的焦点问题是把变量包含到变量……好的,那么你应该真正地关注<代码>包含()/CUT>以及它如何处理返回值,具体的例子是4和Berry。接下来的答案是使用答案而不是评论…总之,谢谢JavaGrand。我不认为链接到手册本身就是一个答案:它是有效的。谢谢,Berry。我不知道奥布的事。。而且我还没有尝试过ob!。。呵呵,我将尝试开始学习ob.*。。谢谢