php变量作用域外循环

php变量作用域外循环,php,mysql,sql,Php,Mysql,Sql,我试图在循环内保存一些结果,然后将名为$hello的外部变量修改为'123',然后在循环外打印它 $hello = 'hi'; // SQL // if ($stmt = $this->dbCon->prepare($sql)) { //$stmt->bind_param("s", $chainid); call_user_func_array(array($stmt, 'bind_param'),

我试图在循环内保存一些结果,然后将名为$hello的外部变量修改为'123',然后在循环外打印它

$hello = 'hi';
        // SQL //
        if ($stmt = $this->dbCon->prepare($sql)) {
            //$stmt->bind_param("s", $chainid);
            call_user_func_array(array($stmt, 'bind_param'), $params);
            $stmt->execute();
            $stmt->bind_result($sysid, $branchid, $harddiscount);
            while ($stmt->fetch()) {
                $harddiscount = number_format($harddiscount / 100, 2) . "€";

                //echo $couponname ."*".$trustanduseid."({$clientid})*".$productname."*".$chainname."*".$branchname."*".$receiptdate."*".$companyname."*".$brandname."*".$receiptid."*".$counterid."*".$discountvalue."*";
                //echo $couponname ."*".$trustanduseid."({$clientid})*".$productname."*".$chainname."*".$branchname."*".$receiptdate."*".$companyname."*".$brandname."*".$receiptid."*".$counterid."*".$harddiscount."*";

                $hello = '123';
            }
            $stmt->close();
        }

        echo $hello;
上面打印的是“hi”,而不是“123”


我们如何解决它呢?

可能是您的代码在$stmt->fetch{时没有进入的问题。否则,它将为$hello打印123。您的if或while条件为false,这不应该工作吗?$hello='hi';$output[{$hello}]='asdf';echo$hi;