2014-07-06 62 views
-1

我有一個簡單的PHP代碼,像這樣的: 使用PHP從一個變量包括:

<div id="master"> 
    <div id="info"> 
     <?php include 'division.php'; 
     echo $winrate; 

     ?> 
    </div> 

    <div id="lastmatches"> 
     <?php include 'lastmatches.php';?> 
    </div> 



</div> 

</body> 

正如你看到的,我想呼應$勝率,但勝率$是來自lastmatches.php變量。所以它永遠不會工作。有人有一個想法在div信息中回顯$ winrate?我卡住了,我希望你們能幫助我!

在此先感謝!

+1

'$ winrate'在將來定義時不能顯示。在回聲之前加入lastmatches.php。 –

+0

複製http://stackoverflow.com/questions/4675932/passing-a-variable-from-one-php-include-file-to-another-global-vs-not 使用'global $ winrate;' –

+0

但是,如果我早些時候將它包含在HTML和CSS的問題中呢? – Chiel

回答

1

您需要在之前包含lastmatches.php才能定義$winrate。 但是,如果這個文件輸出了一些內容,那麼你會希望使用緩存系統在正確的位置輸出正確的內容。

<div id="master"> 
    <div id="info"> 
     <?php include 'division.php'; 
     // begin cache 
     ob_start(); 
     include 'lastmatches.php'; 
     // end cache 
     $lastmatchescontent = ob_get_clean(); 
     echo $winrate; 

     ?> 
    </div> 

    <div id="lastmatches"> 
     <?php echo $lastmatchescontent; ?> 
    </div> 



</div> 

</body> 
+0

我會稱之爲輸出緩衝而不是緩存,因爲它不保存頁面來節省處理時間。 –

+0

事實上,這是我第一次聽說這種方法,它被稱爲「緩存」,所以它留在我心中。但是你的名詞更確切。 –