Ian*_*ugh -3 php variables static
我正在编写一个简单的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;
}
}
Run Code Online (Sandbox Code Playgroud)
这回来了......
inResults:Player 00000000000000000000000000000000000000000000000000000000000000000000000000000
这是因为您使用的是+代替.连接.
$htmlString .= "<tr><td>"+$KeyObject+"</td></tr>";
Run Code Online (Sandbox Code Playgroud)
替换+为.:
$htmlString .= "<tr><td>".$KeyObject."</td></tr>";
Run Code Online (Sandbox Code Playgroud)
+将你的字符串变成整数(返回0)并通过完全有效的方法将零连接到字符串的其余部分$htmlString += [string here].