Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 20-11-2008
Member
 
Join Date: Oct 2008
Posts: 37
Problem with a foreach loop in PHP

I started PHP few days ago so sorry if my question is stupid, but there is something I do not understand. Thank you in advance for your indulgence.

In fact, I try to make a foreach loop that adds all the values of a variable Array ($ result) and register it in a variable non-Array ($ nb_vote).

So that's what I did, but it does not and I do not understand why:
Code:
 $ fp_resultat_sondage = fopen ( "resultat_sondage.txt", "r"); 
	  rewind ($ fp_resultat_sondage); 
	  $ lecture_resultat = fread ($ fp_resultat_sondage, filesize ( "resultat_sondage.txt")); 
	  fclose ($ fp_resultat_sondage); 
	  $ result = explode (",", $ lecture_resultat); 
	  foreach ($ results as $ value) ( 
		  echo $ value. "<br />"; 
		  $ nb_vote $ nbvote + = $ value; 
	  ) 
	  echo $ nb_vote;
The result values are $ 31, 50 and 63.
But at the end of the script, the browser displays only 63.
Reply With Quote
  #2  
Old 20-11-2008
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: Problem with a foreach loop in PHP

Perhaps you've already reported on the source of your problem.

The error is more obvious: $ nb_vote $ nbvote + = $ value;

Instead of $ nb_vote $ nb_vote + = $ value;

Moreover, even if the thing with php is not entirely useful, set the meter by $ nb_vote = 0 before starting the loop seems to be more respectful to the art of programming.
Reply With Quote
  #3  
Old 20-11-2008
Member
 
Join Date: Oct 2008
Posts: 37
Re: Problem with a foreach loop in PHP

lol but it's not true?

thank you very much, Praetor, I had not seen anything at all...

Finally, I tried: $ nb_vote + = $ value;
and because it was market, I spent another thing.

Finally, in any case, thank you very much!
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem with a foreach loop in PHP"
Thread Thread Starter Forum Replies Last Post
Game loop problem with XNA Botan Software Development 5 30-11-2010 05:35 AM
which is better List<T>.ForEach or custom IEnumerable<T>? MACE Software Development 5 19-02-2010 09:56 PM
Foreach statement of C sharp Ram Bharose Software Development 5 19-01-2010 08:42 AM
Problem with while loop that never stops Mindstorm Software Development 3 26-10-2009 04:53 PM
Javascript ForEach loop equivalent Ikshud Software Development 2 04-08-2009 07:36 PM


All times are GMT +5.5. The time now is 04:21 AM.