TechArena Community

TechArena Community (
-   Software Development (
-   -   Problem with a foreach loop in PHP (

JiJi 20-11-2008 04:54 PM

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:

$ 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.

Praetor 20-11-2008 04:55 PM

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.

JiJi 20-11-2008 05:57 PM

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!

All times are GMT +5.5. The time now is 07:09 AM.