Login | Register | FAQ
Anonymous

need understanding

+ Post a reply

3 posts Page 1 of 1


need understanding

by alanthenovice » Mon Jul 25, 2011 4:32 am

$x = 1;

while ($x <= 50) {
echo "<p>This text is repeated 50 times</p>";
$x = $x + 1;
}
?>

1 is stored as the value for the variable $x, right?
Then you used a while loop and set the condition for the loop to be that the value of the same variable, $x can be less then or equal to 50, right? How am I doing so far?

Then you used the string function echo to write a string to the server. This is what the browser will show.

The initial value of $x is "1", then after the string function echo you change the value of $x to be $x+1; therefore, in plain English, "the initial value of $x is 1 and while the condition of $x is <= 50, echo "This text is repeated 50 times" under the additional condition that $x is going to continually be added to by the integer of 1."

What if you didn't add the $x = $x + 1;?

Would there be an error due to an undefined status, or would the website just go on to infinitY?

Almost scared to try it...

I think I understand. Let me know if any of my reasoning is off. I just want to understand so that this is easier to me. I could memorize it, but I like the puzzle.

Thanks,
Alan
User avatar

alanthenovice

  • Posts: 16
  • Joined: Mon Jun 13, 2011 5:18 pm

Re: need understanding

by alanthenovice » Mon Jul 25, 2011 4:45 am

Okay, so I did it, and at first it was cool because the text kept repeating over and over and I scrolled down for a while. But then the screen went all white. Don't know what that is all about, but I x'd out really quick.
User avatar

alanthenovice

  • Posts: 16
  • Joined: Mon Jun 13, 2011 5:18 pm

Re: need understanding

by HTML.net » Mon Jul 25, 2011 8:27 am

Yep. It will just continue to write "This text is repeated 50 times" (because the variable $x remains under 50) till the scripts time out.

- Andreas, HTML.net
User avatar

HTML.net
Site Admin

  • Posts: 1184
  • Joined: Sat Sep 11, 2010 11:24 am
--
Show some love for HTML.net on Twitter, Facebook and Google: Use the buttons on top of all pages.


+ Post a reply

Page 1 of 1

?>