Adding values from a php string

0

I have the following code:

foreach ($class->ListaChave($CdLote) as $dados) {
   var_dump($dados->getQvol());
}

Output:

string(1) "1"

string(1) "1"

string(1) "2"

string(1) "1"

I need these values to be summed and to return the value inside a variable.

    
asked by anonymous 31.08.2017 / 15:30

1 answer

1

You can do the sum as usual. But if you want to ensure that the return of getQVol() is always integer, and you do not have access to the class to change, you can do so:

<?php
$soma = 0;
foreach ($class->ListaChave($CdLote) as $dados) {
   $soma += (int) $dados->getQvol();
}

echo $soma;
    
31.08.2017 / 16:15