I am writing a simple PHP class, but when I am trying to use a variable inside of a foreach loop, it is printing out 0 (null). But, when I echo it out right before that loop, it prints the correct value. Any thoughts?
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;
}
}
This returns…
inResults: Player
00000000000000000000000000000000000000000000000000000000000000000000000000000000000
This is because you’re using
+instead of.for concatenation.Replace the
+with a.:+will juggle your strings into integers (returning 0) and concatenate that zero to the rest of your string through the totally valid$htmlString += [string here].