Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

number_format() throws a TypeError because the value is a string

Errornumber_format() expects parameter 1 to be float, string given

Short 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.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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));
  2. 2Match what you see, then jump to its fix

    1. 1var_dump shows string(0) "", or text such as "N/A" or "$12.00"The value is an empty or non-numeric stringFix
    2. 2A numeric string such as "1234.50" also fails, in a file that starts with declare(strict_types=1)declare(strict_types=1) is set in the calling fileFix

What does the error look like?

  • number_format() expects parameter 1 to be float, string given
  • Uncaught 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.php

Confirmed by php.net

Did this fix it?

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.php

2Fix

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.php

Confirmed by php.net

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.