Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP number_format not working [closed]

Tags:

php

I'm making a website and I've got the following problem. I'm importing query's from MySql, but to make it easier to understand I'm using local arrays now.

<?php 
            $productnames = array("Raspatat oorlog", "Raspatat mayo", "Raspatat pizza", "Raspatat ui", "Raspatat curry");
            $productprices = array(1.7, 2.3, 2.45, 3.45, 34.669);

            echo "<table>
                    <tr>
                        <td>Product</td>
                        <td>Prijs</td>
                    </tr>";
            for($i =0; $i < sizeof($productnames); $i++) {
                $iprice = number_format((float)$pruductprices[$i], 2, ',', '');
                echo "<tr><td>$productnames[$i]</td>
                <td>$iprice</td></tr>"; 
            }

            echo "</table>";
            echo $iprice;
        ?>

The code is running, but in my table i get a price of 0,00 everywhere. Does anyone know why?

Thanks!

(this is the table

Product         Prijs
Raspatat oorlog |0,00
Raspatat mayo   |0,00
Raspatat pizza  |0,00
Raspatat ui     |0,00
Raspatat curry  |0,00

}

like image 405
Rik Nijhuis Avatar asked Aug 10 '26 13:08

Rik Nijhuis


1 Answers

You have a typo in your code (around your variable pruductprices name):

<?php 
    $productnames = array("Raspatat oorlog", "Raspatat mayo", "Raspatat pizza", "Raspatat ui", "Raspatat curry");
    $productprices = array(1.7, 2.3, 2.45, 3.45, 34.669);

    echo "<table>
    <tr>
    <td>Product</td>
    <td>Prijs</td>
    </tr>";
    for($i =0; $i < sizeof($productnames); $i++) 
    {
        $iprice = number_format((float)$productprices[$i], 2, ',', '');
        echo "<tr><td>$productnames[$i]</td>
        <td>$iprice</td></tr>"; 
    }

    echo "</table>";
    echo $iprice;
?>

Output:

 Product    Prijs
Raspatat oorlog     1,70
Raspatat mayo   2,30
Raspatat pizza  2,45
Raspatat ui     3,45
Raspatat curry  34,67
34,67

If you want to easily debug a script, add this to the top:

error_reporting(E_ALL);
ini_set('display_errors', 1);

It will show you all the whoopsie-daisies and makes for debugging bliss.

like image 154
Fluffeh Avatar answered Aug 13 '26 03:08

Fluffeh