Count result of a variable

Asked

Viewed 197 times

1

Colleagues. I do not know if I was clear in my title, but I will try to explain here. I have the following code:

    foreach($notasAlunos as $notaAluno){                                   
    $resXML = $xml->avaliacao->disciplina->questao->resposta;
    if($resXML == $notaAluno){
        $valor = 1; 
        echo count($valor);
    }else{
        $valor = 0;
    }
    //echo $valor;
  }

I need to add the amount of "1" that the $value variable brings us. I tried to use Count($value), but it is bringing "111". How would I add up these values and return 3?

1 answer

1


Thus use:

    foreach($notasAlunos as $notaAluno){                                   
       $resXML = $xml->avaliacao->disciplina->questao->resposta;
       if($resXML == $notaAluno){
           $valor++;           
       }else{
           $valor = 0;
        }

    }
    echo $valor;

In your code you are always setting the variable $valor with 1;

In my code I’m always incrementing + 1;

  • Hello Tiago. I did so, but it returns now 1 2 and not just 2

  • I changed the code, put the echo outside the foreach

  • It now returns no value

  • Change the place of echo $value, put between the last two keys

  • Hello diego, but put where exactly? inside the loop?

  • It has to be out of the loop

  • Hi Tiago. I did as mentioned, but unfortunately does not return values. It is null.

  • You have to start the variable with zero $valor = 0 before increasing.

  • I started and also nothing...

  • @Jose Marcos start off outside the for, see an example: https://ideone.com/7rKta6

  • Maybe he’s not getting into the loop

  • @Jose Marcos to start $valor = 0 outside the for, remove the internal Else from if. So, even if it doesn’t enter, the result will be zero.

  • Using the tool @Diegof recommended http://ideone.com/hcLO6I this example works.

  • Tiagos is sure it works? PHP Notice: Undefined variable: valor in /home/1IA14d/prog.php on line 4

  • have, please look at stdout the result.

  • See the error that returns.

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.