我正在编写一个简单的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;
}
}
这会返回......
inResults:Player 00000000000000000000000000000000000000000000000000000000000000000000000000000000000
答案 0 :(得分:5)
这是因为您使用+
代替.
进行连接。
$htmlString .= "<tr><td>"+$KeyObject+"</td></tr>";
将+
替换为.
:
$htmlString .= "<tr><td>".$KeyObject."</td></tr>";
+
将juggle your strings into integers(返回0)并通过完全有效的$htmlString += [string here]
将该零连接到字符串的其余部分。