Results 1 to 3 of 3

Thread: Problem with a foreach loop in PHP

  1. #1
    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.

  2. #2
    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.

  3. #3
    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!

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,989,926.19209 seconds with 17 queries