How to solve a non well formed numeric value encountered in PHP?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How to Solve a Non-Well Formed Numeric Value Encountered in PHP Calculations
As a senior developer, I frequently encounter errors related to data types and mathematical operations in PHP. The error message, "A non well formed numeric value encountered," is frustrating because it points to a logical failure, but often the true culprit lies in how data entered into your script was handled—specifically, trying to perform arithmetic on a value that isn't actually a number.
This post will diagnose why you are seeing this error in your calculation snippet and provide robust solutions using modern PHP practices.
Understanding the Error: Why PHP Fails at Math
The error "A non well formed numeric value encountered" occurs when PHP attempts to perform an arithmetic operation (like addition, multiplication, or division) on a variable that contains a string, null, an array, or some other non-numeric value. PHP tries its best to "juggle" these types, but when it hits a definitive non-numeric object during a calculation, it throws this error to prevent unpredictable results.
In your specific code snippet:
$tax = ($tax_rate * $sub_total) / 100;
// Here I am having an error - A non well formed numeric value encountered
$tax = number_format($tax, 2); // <--- Potential issue point
$grand_total = $tax + $sub_total;
While the error might appear on the line where you format the tax, the root cause is almost certainly that $sub_total (or one of the variables used in the calculation leading up to it) was not a valid number when the multiplication or division occurred. This usually happens because input data from forms or databases comes back as strings, even if they look like numbers (e.g., "100.50").
The Root Cause: Data Type Mismatch
The most common scenario is that $sub_total was inadvertently loaded as a string instead of a float or integer when the initial calculation began. When you multiply strings, PHP might handle it, but complex operations involving non-numeric data lead to this failure.
The fundamental rule for robust programming in Laravel and general PHP development is: Never trust user input. You must validate and sanitize every piece of data before you use it in calculations.
The Solution: Input Validation and Strict Casting
To fix this, we need to implement strict validation at the point where the data enters your calculation logic. We will focus on ensuring that all variables involved in financial math are explicitly cast to floating-point numbers (float) after ensuring they are numeric.
Step 1: Validate and Sanitize Inputs
Before you start the calculations, check if the necessary values exist and ensure they are numeric. Using functions like is_numeric() or stricter type casting is essential.
Step 2: Implement Safe Arithmetic
We must force the variables to be numbers before performing arithmetic operations. This prevents unexpected string behavior from causing a fatal error later on.
Here is how you can refactor your code to handle this safely:
// Assume $item and $product arrays contain raw data from input sources.
// 1. Ensure all inputs are treated as numbers, defaulting to 0 if invalid
$quantity = (float)($item['quantity'] ?? 0);
$price = (float)($product['price'] ?? 0.00);
// Calculate sub_total safely
$sub_total = $price * $quantity;
// 2. Calculate tax, ensuring the result is a float
$tax_rate = 18;
$tax = ($tax_rate * $sub_total) / 100;
// 3. Formatting (Separate calculation from presentation)
// Only format for display later. Keep the calculated value as a float for further math.
$formatted_tax = number_format($tax, 2);
// Calculate grand total using the precise float values
$grand_total = $sub_total + $tax; // Use the unformatted tax for accurate totals
echo "Sub Total: " . number_format($sub_total, 2) . "\n";
echo "Tax Amount: " . $formatted_tax . "\n";
echo "Grand Total: " . number_format($grand_total, 2) . "\n";
Best Practice: Using Laravel's Eloquent for Data Integrity
When dealing with database-driven data in a framework like Laravel, leveraging Eloquent models helps enforce data types. By defining your model attributes as numeric types (or using appropriate casting within your models), you shift the responsibility of data integrity to the ORM layer, making your controller logic cleaner and safer. This aligns perfectly with the principle of building robust systems, similar to how dependency injection is used when structuring code in Laravel.
Conclusion
The error "A non well formed numeric value encountered" is a signal that your data pipeline is introducing non-numeric values into mathematical operations. The solution is not just fixing one line, but adopting a defensive programming mindset: Validate, Sanitize, and Cast every piece of external data before it touches your calculations. By explicitly casting inputs to float and ensuring all intermediate variables are numeric, you ensure that your financial calculations are accurate, predictable, and free from runtime errors. Always prioritize data integrity above all else!