how to put include(phpfile) to variable?

狂风中的少年 提交于 2019-12-11 03:45:55

问题


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.

example here:

main code:

$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 code:

hello <?php echo $name; ?>

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


回答1:


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


来源:https://stackoverflow.com/questions/6621796/how-to-put-includephpfile-to-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!