php – Error comparing two numbers

Question:

I'm having a problem comparing two numbers, here's my code:

// Valor
    $total_ch = 691.11;

    // Calcula valor total das contas a receber
    $result = $mysqli->query("SELECT SUM(valor) FROM contas_receber where status = '1' and id IN ($contas)");
    $row = mysqli_fetch_array($result);
    $valor_contas =  $row['SUM(valor)'];

    // Verifica se o total dos cheques e maior que os das contas a receber
    if ($valor_contas > $total_ch) {
        // Exibe Alert
        echo "erro";
    }

The returned value of $row['SUM(valor)'] is 691.11. Therefore, the two values ​​are the same, but the system is pointing out that the value of valor_contas is greater than that of total_ch .

What is causing this? How to solve?

Answer:

Cast the string to float using (float) , like this:

$valor_contas =  (float) $row['SUM(valor)'];
Scroll to Top
AllEscort