number_format() throws a TypeError because the value is a string
number_format() expects parameter 1 to be float, string givenShort answer
In PHP 8, number_format() has a float parameter. A numeric string such as 12.50 is converted, but an empty or non-numeric string like N/A or $12 throws a TypeError. With strict_types on, any string fails. Check the value with is_numeric() and cast it to float before formatting.
Fix it fast
1Find out which cause you have
Print the exact value and type that reaches number_format() just before the call. The error message does not show the value, and the empty string and a currency text look alike in a log.
var_dump($price); var_dump(is_numeric($price));2Match what you see, then jump to its fix
What does the error look like?
number_format() expects parameter 1 to be float, string givenUncaught TypeError: number_format(): Argument #1 ($num) must be of type float, string given
Cause 1 of 2
The value is an empty or non-numeric string
1Check
A float parameter accepts a numeric string, but an empty string is not numeric, and text with a currency sign is not numeric either. Rows with no price from a form or a database often arrive as an empty string.
var_dump(is_numeric(''));
var_dump(is_numeric('$12.00'));
var_dump(is_numeric('12.50'));2Fix
Test the value first and decide what an empty price means on the page, then format only numbers. Strip symbols from the text before the check if you have to accept them.
$amount = is_numeric($price) ? (float) $price : 0.0;
echo number_format($amount, 2);3Verify
Run the page with an empty value and with a normal one; both should print without an error.
php -l welcome.phpConfirmed by php.net
Cause 2 of 2
declare(strict_types=1) is set in the calling file
1Check
In strict mode only an int may widen to float. Values read from a database driver, a form or JSON are often strings, so every call fails.
grep -n "strict_types" welcome.php2Fix
Cast the value at the call, so the function receives a float.
echo number_format((float) $row['total'], 2);3Verify
Reload the page; the amount prints with two decimals.
php -l welcome.phpConfirmed by php.net
How do I stop it coming back?
- Convert database and form values to numbers once, where you read them, not at every echo.
- Store money in an integer column of the smallest unit or in DECIMAL, and format only at display time.
- Use declare(strict_types=1) with typed functions so a wrong type fails in your own code first.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix