Jump to content

simple while statement problem


seadonkey

Recommended Posts

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
   <title>While Logic</title>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>

<?php
$Count = 0;
while ($Count > 100){
$Numbers[] = $Count;
++$Count;
foreach ($Count as $CurNum)
echo"<p>$CurNum</p>";

?>

</body>

</html>

 

 

 

I am unable to debug an error that is looking me in the face. Its a simple print the numbers 1-100 as long as the number is above zero.

Link to comment
https://forums.phpfreaks.com/topic/91414-simple-while-statement-problem/
Share on other sites

what you code is doing is checking if the $Count is greater than 100, if it is then it is going to loop, you should be using a '<' not a '>', and also, for this kind of looping you would be better off using a for loop:

<?php

for ($i=0; $i <= 100; $i++)
{
    $Numbers[] = $i;
}
echo implode("<br />" $Numbers);
?>

gotcha, I knew it was something very simple however there is also a problem with my foreach

<?php

$Count = 0;

while ($Count < 100){

$Numbers[] = $Count;

++$Count;

foreach ($Count as $CurNum)

echo"<p>$CurNum</p>";

}

?>

 

I am getting a Invalid argument supplied for foreach() I am assuming I am getting that error because I attempting to us a foreach with $CurNum that is not an array? Or have I completely missed the boat.

No basically what foreach does is loop through each different value of your array and set it to in this case $CurNum, I don;t know why you have it inside your while loop, that isnt needed.

<?php
$Count = 0;
while ($Count < 100)
{
    $Numbers[] = $Count;
    ++$Count;

}

foreach ($Count as $CurNum)
{
    echo "<p>$CurNum</p>";
{
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.