簡體   English   中英

如何將包含(phpfile)放入變量?

[英]how to put include(phpfile) to variable?

我想要像默認一樣的主題或模板,然后其他 php 可以使用它作為他們的主題。 但我有一個問題,我無法將主題放入 output 打印之前生成的變量。

這里的例子:

主要代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM