我正在尝试使循环变量在循环外可访问。一切正常,但这段代码有问题:

$dayVar."_count" = $dayVarCount;

完整代码如下:
<?php

$day_1="sep_28";
$day_2="sep_29";
$day_3="sep_30";

$query = mysql_query("SELECT * FROM table WHERE id = '$id'");

while ($row = mysql_fetch_assoc($query))
{

for ($i = 1; $i <= 3; $i++)
    {
        $dayVar = "day_".$i;
        $dayVarCount = $row[$$dayVar];
        echo $$dayVar . ': ' . $dayVarCount . '<br>';

        $dayVar."_count". = $dayVarCount;
    }
}

echo "$day_3_count";

?>

最佳答案

我认为您对$dayVar的变量赋值“\u count”不正确或非法。我会改为尝试${$dayVar.“\u count”}。

关于php - 使变量在循环外可访问,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12669242/

10-09 20:10