如何把include(phpfile)放到变量? [英] how to put include(phpfile) to variable?

查看:132
本文介绍了如何把include(phpfile)放到变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像主题或模板一样未触及默认,然后其他PHP可以使用它作为他们的主题。但我有问题,我不能把主题变为输出打印前生成的变量。

I want like theme or template untouched like default then other php can use it as their theme. but i have problem that i cant put theme to variable with generated before output print.

示例:

主要代码:

$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代码:

theme.php code:

hello <?php echo $name; ?>

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

NOTE: i know while loop will break but this is just example code. because i use while(mysql_fetch_array($result)). thanks

推荐答案

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

That code can't possible work, so I reckon it's pseudo code. Anyway, the issue is fixed with use of the ob_* functions in PHP, as follows. This is how the majority of template parsers in php work:

<?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
 */

这篇关于如何把include(phpfile)放到变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆