RE: [PHP] Else/For loop
> Being relatively new to php, I discovered the following > after about 2 hours of debugging last night and was wondering why??? > > I had the following code: > > if ( $num_results = 0 ) > { > echo "no records found"; > } > else > { > for ($i=0; $i < $num_results; $i++) > { > display record set > } > } > The duduction...for loops will not work inside else > statements. The question...why? Your deduction is wrong, they do. If this is the code that you're actually using then I suspect that your problem is more in the conditional you've got in your 'if' statement. You aren't doing a compare you're actually setting $num_results to 0. A comparison is '==' rather than just '. -- -- Outback Queensland Internet Longreach ** New Web Site Online * Queensland Australia W: www.outbackqld.net.au -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Else/For loop
Thanks to everyone who replied. Of course it should be '=='. I was too focused on why the for was not working and overlooked the obvious. Mark Roberts Sr. Systems Analyst LanApps/Web Development The Williams Information Services Corporation 918-573-1706 [EMAIL PROTECTED] -Original Message- From: Christoph Starkmann [mailto:[EMAIL PROTECTED]] Sent: Wednesday, June 12, 2002 8:27 AM To: Roberts, Mark; '[EMAIL PROTECTED]' Subject: RE: [PHP] Else/For loop Hi Mark! > Being relatively new to php, I discovered the following after > about 2 hours > of debugging last night and was wondering why??? > > I had the following code: > > if ( $num_results = 0 ) > { > echo "no records found"; > } > else > { > for ($i=0; $i < $num_results; $i++) > { > display record set > } > } > > As a result, the for loop never executed, no matter how many > records were > returned in the record set. I placed echo statements though > out to determine > why the for loop was not being executed. I put an echo > statement just before > the for to make sure the else condition was being met. > > Finally, I just copy/pasted (literally) the for loop to just > before the 'if' > statement. Much to my surprise...the for loop now works > perfectly. (Except, > of course, if there are no records returned). > > The duduction...for loops will not work inside else statements. The > question...why? I guess this would be the end of all PHP-programming ;) Of course you can place any correct block of code inside any other correct block of code. But, i your case $num_results IS zero. Just place an echo statement inside the if block ;) Very frequent error ;) "if ($num_results = 0)" indeed is an asignment, no comparison. Better use "==" if you want to compare thingies... > I think I can work around this with some different coding, > but why should I > have to? I have done this same thing in java, perl and other > languages... Yes, also with "==" ;) Cheers, Kiko -- It's not a bug, it's a feature. christoph starkmann mailto:[EMAIL PROTECTED] http://www.gruppe-69.com/ ICQ: 100601600 -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Else/For loop
Hi Mark! > Being relatively new to php, I discovered the following after > about 2 hours > of debugging last night and was wondering why??? > > I had the following code: > > if ( $num_results = 0 ) > { > echo "no records found"; > } > else > { > for ($i=0; $i < $num_results; $i++) > { > display record set > } > } > > As a result, the for loop never executed, no matter how many > records were > returned in the record set. I placed echo statements though > out to determine > why the for loop was not being executed. I put an echo > statement just before > the for to make sure the else condition was being met. > > Finally, I just copy/pasted (literally) the for loop to just > before the 'if' > statement. Much to my surprise...the for loop now works > perfectly. (Except, > of course, if there are no records returned). > > The duduction...for loops will not work inside else statements. The > question...why? I guess this would be the end of all PHP-programming ;) Of course you can place any correct block of code inside any other correct block of code. But, i your case $num_results IS zero. Just place an echo statement inside the if block ;) Very frequent error ;) "if ($num_results = 0)" indeed is an asignment, no comparison. Better use "==" if you want to compare thingies... > I think I can work around this with some different coding, > but why should I > have to? I have done this same thing in java, perl and other > languages... Yes, also with "==" ;) Cheers, Kiko -- It's not a bug, it's a feature. christoph starkmann mailto:[EMAIL PROTECTED] http://www.gruppe-69.com/ ICQ: 100601600 -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Else/For loop
If you still have an error there, change if ($num_results = 0) to if ($num_results == 0) See the difference? Realize the error? Niklas -Original Message- From: Roberts, Mark [mailto:[EMAIL PROTECTED]] Sent: 12. kesäkuuta 2002 16:20 To: [EMAIL PROTECTED] Subject: [PHP] Else/For loop Being relatively new to php, I discovered the following after about 2 hours of debugging last night and was wondering why??? I had the following code: if ( $num_results = 0 ) { echo "no records found"; } else { for ($i=0; $i < $num_results; $i++) { display record set } } As a result, the for loop never executed, no matter how many records were returned in the record set. I placed echo statements though out to determine why the for loop was not being executed. I put an echo statement just before the for to make sure the else condition was being met. Finally, I just copy/pasted (literally) the for loop to just before the 'if' statement. Much to my surprise...the for loop now works perfectly. (Except, of course, if there are no records returned). The duduction...for loops will not work inside else statements. The question...why? I think I can work around this with some different coding, but why should I have to? I have done this same thing in java, perl and other languages... Mark Roberts Sr. Systems Analyst LanApps/Web Development The Williams Information Services Corporation 918-573-1706 [EMAIL PROTECTED] ### This message has been scanned by F-Secure Anti-Virus for Internet Mail. For more information, connect to http://www.F-Secure.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Else/For loop
Wouldn't you need if ( $num_results == 0 ) not if ( $num_results = 0 ) if you do the latter, it will allways be true, because you're setting the var... easy mistake to make! check out comparisons in the manual for more info. Justin French on 12/06/02 11:19 PM, Roberts, Mark ([EMAIL PROTECTED]) wrote: > Being relatively new to php, I discovered the following after about 2 hours > of debugging last night and was wondering why??? > > I had the following code: > > if ( $num_results = 0 ) > { > echo "no records found"; > } > else > { > for ($i=0; $i < $num_results; $i++) > { > display record set > } > } > > As a result, the for loop never executed, no matter how many records were > returned in the record set. I placed echo statements though out to determine > why the for loop was not being executed. I put an echo statement just before > the for to make sure the else condition was being met. > > Finally, I just copy/pasted (literally) the for loop to just before the 'if' > statement. Much to my surprise...the for loop now works perfectly. (Except, > of course, if there are no records returned). > > The duduction...for loops will not work inside else statements. The > question...why? > > I think I can work around this with some different coding, but why should I > have to? I have done this same thing in java, perl and other languages... > > Mark Roberts > Sr. Systems Analyst > LanApps/Web Development > The Williams Information Services Corporation > 918-573-1706 > [EMAIL PROTECTED] > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php