Need help with a while statement

Hi, I’m very new to PHP and when I run this code, my page is blank. It’s supposed run through an array with 1-100.
[php]
$Count= 0;
while ($Count > 100) {
$Numbers[] = $Count;
++$Count;
foreach ($Count as $CurNum)
echo “

$CurNum

”;
}
[/php]

Thank you, I’m sorry if it seems so simple, but I just don’t get how to do these. If I see it the right way, I will understand more.

Stef

If you’re new to PHP, I would get into the habit of putting your code into brackets and using indentation:

[php]$Count = 0;

while ($Count > 100) {
$Numbers[] = $Count;
++$Count;
}

foreach ($Count as $CurNum) {
echo “

$CurNum

”;
}[/php]

Also, the $Count variable will never be over 100 (you’re using the greater than > operator). It should be less than:

[php]while ($Count < 100) {
$Numbers[] = $Count;
++$Count;
}[/php]

In the foreach, the argument before the as should be the array. You’re using the $Count variable (which is a number) - it should be the $Numbers array:

[php]foreach ($Numbers as $CurNum) {
echo “

$CurNum

”;
}[/php]

As well as this, you should define the array before you use it:

[php]$Numbers = array();[/php]

Giving you:

[php]$Numbers = array();
$Count = 0;

while ($Count < 100) {
$Numbers[] = $Count;
++$Count;
}

foreach ($Numbers as $CurNum) {
echo “

$CurNum

”;
}[/php]

This works as expected on codepad: http://codepad.org/kFFKVX7B

Sponsor our Newsletter | Privacy Policy | Terms of Service