php foreachtest.php
Content-type: text/html
X-Powered-By: PHP/4.3.3
<br />
<b>Warning</b>: Invalid argument supplied for foreach() in <b>/opt/lampp/htdocs/unittest/foreachtest.php</b> on line <b>20</b><br />
This is foreachtest.php:
<?php
$portArray;
function test() {
for ($unit = 0; $unit < 3; $unit++) {
$GLOBALS['portArray'][$unit] = $unit;
}
}
if ($SERVER["REQUEST_METHOD"] == "GET")
{
test();
} else if ($SERVER["REQUEST_METHOD"] == "POST")
{
//...
test();
}
foreach ($portArray as $i) {
echo "i $i";
}
?>
The web browser display the data:
i 0i 1i 2
Why the warning?
How to fix this problem?