我正在编写一个简单的 PHP 类,但是当我尝试在 foreach 循环中使用变量时,它打印出 0(null)。但是,当我在该循环之前回显它时,它会打印正确的值。有什么想法吗?
class Search
{
public static $KeyObject=null;
//...KeyObject is assigned some value...
public function resultsToHTML()
{
$KeyObject = $this->KeyObject;
echo "inResults: $KeyObject <br />";
$htmlString = "";
if(!empty($this->resultList))
{
$htmlString .= "<table><th>Results</th><tbody>";
foreach($this->resultList as $row)
{
$htmlString .= "<tr><td>"+$KeyObject+"</td></tr>";
$htmlString .= "<tr>";
foreach($row as $key => $value)
{
$htmlString .= "<td class=\"$key\" id=\"$value\">$value</td>";
}
$htmlString .= "</tr>";
}
$htmlString .= "</tbody></table>";
}
return $htmlString;
}
}
这返回...
结果:玩家 000000000000000000000000000000000000000000000000000000000000000000000000000000000000