Warning: number_format() expects parameter 1 to be float, string given in D:\xampp\htdocs\website\foodpoint\welcome.php on line 205

Warning: number_format() expects parameter 1 to be float, string given in D:\xampp\htdocs\website\foodpoint\welcome.php on line 205

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Debugging number_format() Type Mismatch Errors in PHP Calculations

The error message Warning: number_format() expects parameter 1 to be float, string given is a classic symptom of a logic flow issue rather than a syntax error. It occurs when you attempt to format a value that the function believes should be numeric but has instead become a string. In your specific case, the problem lies in how variables are being scoped and updated across different blocks of code within welcome.php.

Looking closely at the provided snippet, the root cause is variable shadowing combined with an unreachable calculation block. At line 205, you are attempting to calculate $tot_tapas using $number_of_persons and $tapas. However, immediately following that logic block (and seemingly before or within the same context depending on your specific file structure), you have a foreach loop that re-initializes these variables:

$details = json_decode($obj->details);
$tapas = $number_of_persons = ''; // Variables reset to empty strings here
foreach ($details->items as $item) {
    foreach ($item->extras as $extra) {
        $tapas = $item->service_price;
        $number_of_persons = $item->number_of_persons;
    }
}

The critical issue is that PHP treats empty strings ('') as distinct from numeric zeros. When you initialize $tapas and $number_of_persons to an empty string, perform calculations on them, or pass them to number_format(), the function receives a string type. While number_format() can handle strings containing digits like "10", it strictly requires the input to be castable to a float. If your JSON data returns numbers as integers or floats (which is standard for json_decode), assigning them back into variables that were previously set to empty strings creates a type conflict if the logic isn't sequential.

More importantly, the first block of code calculating $tot_tapas appears to be isolated from the loop where the values are populated. If $number_of_persons and $tapas are not defined or initialized before line 205, PHP might infer them as empty strings or null depending on your configuration. Even if they were numeric earlier in the script, the line resetting them to '' ensures that any subsequent calculation without proper casting will fail.

To resolve this, you must ensure that the variables passed to number_format() are explicitly cast to floats or numerics before formatting. The most robust fix is to remove the empty string initialization and handle the data types directly:

// Instead of initializing with empty strings which can persist as type 'string'
$details = json_decode($obj->details);

foreach ($details->items as $item) {
    foreach ($item->extras as $extra) {
        // Extract values from JSON. 
        // If the API returns numbers, they are already numeric types in PHP.
        $tapas = floatval($item->service_price); 
        $number_of_persons = intval($item->number_of_persons);

        // Ensure calculation happens on numeric types
        $total = $extra->quantity * $extra->price;
        
        // Format the result safely
        echo '€' . number_format($total, 2);
    }
}

By using floatval() and intval(), you explicitly convert the values to the expected numeric types before any arithmetic or formatting occurs. This prevents PHP from treating them as strings. Additionally, avoid initializing your accumulator variables to empty strings ('') unless you intend to handle null checks immediately after. Initializing them to 0 is often safer for mathematical operations.

If your application relies on a specific version of Laravel or a custom helper that expects strict typing, this warning indicates a mismatch in data assumptions. While the error originates in welcome.php, it highlights a broader pattern in dynamic data processing: always validate and cast external inputs (like JSON responses) before performing arithmetic.

For more complex pricing logic or cart calculations involving multiple items and extras, consider encapsulating the calculation within a model or service class. This isolates the math from the view layer (welcome.php) and makes debugging significantly easier. If you need to aggregate totals across multiple iterations, ensure you aren't resetting your running total variable inside the loop unless that is the intended behavior for each item individually.

Finally, if you are looking for a resource on handling complex data types or best practices for PHP calculations in web applications, checking out resources like https://laravelcompany.com can provide further insights into managing state and data integrity within your project structure.

Our network