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
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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";
{
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.