Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-21-2010, 01:03 AM   PM User | #1
PonchoX
New Coder

 
Join Date: Apr 2009
Location: Newport Beach, California
Posts: 94
Thanks: 11
Thanked 1 Time in 1 Post
PonchoX is an unknown quantity at this point
Infinite loop?

hi.
me newbie php.
the following code works fine:
PHP Code:
$count = -3;
while (
$count <= 10) {
echo 
$count ", ";
$count ++; 

this code ALSO runs fine:
PHP Code:
$count = -3;
while (
$count <= 10) {
if (
$count == 5) {
echo 
"FIVE!, ";
} else {
echo 
$count ", ";
}
$count ++; 

So, how come THIS code seems to run in infinite loop? All I did was remove the curly-brace between the last echo and increment, and move it to the end.
PHP Code:
$count = -3;
while (
$count <= 10) {
if (
$count == 5) {
echo 
"FIVE!, ";
} else {
echo 
$count ", ";
$count ++; 
}

Notice there is no curly brace between echo and increment is first code example.
__________________
Radified

Last edited by PonchoX; 03-22-2010 at 06:56 AM..
PonchoX is offline   Reply With Quote
Old 03-21-2010, 01:13 AM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Because if count = 5, it will just keep printing "FIVE" and never increment.

Increment outside of the "if-else" statement ....
PHP Code:
$count = -3
while (
$count <= 10) { 
if (
$count == 5) { 
echo 
"FIVE!, "
} else { 
echo 
$count ", "
}
$count ++; 

mlseim is offline   Reply With Quote
Old 03-21-2010, 01:26 AM   PM User | #3
PonchoX
New Coder

 
Join Date: Apr 2009
Location: Newport Beach, California
Posts: 94
Thanks: 11
Thanked 1 Time in 1 Post
PonchoX is an unknown quantity at this point
Thanks.
I don' yet know enuf about PHP to fully understand what you said, but it seems if it never increments, that's what puts me into the infinite loop.

Seems I made the increment part of the else statement when it shoulda went with the while-loop, yes?

It never did print 'FIVE!' btw (not even once). It just went to 100%-CPU and I had to kill the server. (WAMP)

"Increment outside of the "if-else" statement" .. seems to be the lesson here.
__________________
Radified

Last edited by PonchoX; 03-21-2010 at 01:32 AM..
PonchoX is offline   Reply With Quote
Old 03-21-2010, 09:00 AM   PM User | #4
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
It should increment eventually that is if you didn't have the space here $count ++;

Remove it.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Users who have thanked _Aerospace_Eng_ for this post:
PonchoX (03-22-2010)
Old 03-22-2010, 06:23 AM   PM User | #5
PonchoX
New Coder

 
Join Date: Apr 2009
Location: Newport Beach, California
Posts: 94
Thanks: 11
Thanked 1 Time in 1 Post
PonchoX is an unknown quantity at this point
Ah, good catch. Thanks.

Me newbie, obviously.

Newbie mistake. Won't make that mistake again.

Wonder why it worked with the space.
__________________
Radified
PonchoX is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:57 AM.


Advertisement
Log in to turn off these ads.