PHP使用include中的变量

PHP使用include中的变量,php,include,Php,Include,我得到了一个简单的PHP代码,如下所示: 如您所见,我希望回显$winrate,但$winrate是一个来自lastmatches.php的变量。所以它永远不会起作用。有些人想在div信息中回显$winrate?我被困住了,希望你们能帮助我 提前谢谢 在定义$winrate之前,需要包含lastmatches.php。 但是,如果此文件输出一些内容,那么您将希望使用缓存系统在正确的位置输出正确的内容 <div id="master"> <div id="info"

我得到了一个简单的PHP代码,如下所示:


如您所见,我希望回显$winrate,但$winrate是一个来自lastmatches.php的变量。所以它永远不会起作用。有些人想在div信息中回显$winrate?我被困住了,希望你们能帮助我


提前谢谢

在定义
$winrate
之前,需要包含
lastmatches.php
。 但是,如果此文件输出一些内容,那么您将希望使用缓存系统在正确的位置输出正确的内容

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

我建议您采用MVC方法,如果您不想使用框架,您可以这样做:


这将使代码更具可读性,将生成输出与从数据库获取数据、解析数据等分离开来。

$winrate
在将来定义时无法显示。在回显之前包含lastmatches.php。重复使用
global$winrate
但是如果我在前面包含它,这是HTML和CSS的一个问题吗?只有在你回显lastmatches.php中的内容的情况下。正确地构造你的代码,这样你就可以得到
$winrate
的值,而不必同时输出一堆HTML。如果你的问题是这两者交织在一起,那么你需要将这两件事解耦。我称之为输出缓冲而不是缓存,因为它不是为了节省处理时间而保存页面。事实上,我第一次听说这种方法时,它被称为
缓存
,所以它一直留在我的脑海中。但你的术语更准确。
<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>