php-src icon indicating copy to clipboard operation
php-src copied to clipboard

PHP Deprecated: number_format(): Passing null

Open conectarsistemas opened this issue 4 years ago • 5 comments

Description

The following code:

$sql = 'SELECT CLIENT.NAME,SUM(X.TOTAL) FROM SALES X WHERE X.CODCLI = SALES.CODCLI) AS TOTAL FROM CLIENT WHERE CLIENT.CODCLI <10';
$rs = mysqli_query($sql);
if ($rs){
	while ($row = mysqli_fetch_array($rs)) {
		echo 'Client: '.$row['NAME'];
		echo 'Total: '.number_format($row['TOTAL'], 2, ",", ".");
	}
}
<?php

Resulted in this output:

Error : PHP Deprecated:  number_format(): Passing null to parameter #1 ($num) of type float is deprecated

But I expected this output instead:

0,00

I'm having problems in PHP 8.1, because of the types and functions like preg_replace, stripos, etc. When I do a SELECT with mysqli_fetch_array, there are fields that will return NULL, and this is generating errors in several points of the system that need to use a trim() for example, but the database return is NULL, which is normal to happen. I believe it should continue to treat NULL as a String type in these cases. The same happens with the number_format if it returns NULL in a SELECT statement SUM(), it reports an error, but this happens straight away.

PHP Version

PHP 8.1.4

Operating System

Windows 10

conectarsistemas avatar Mar 24 '22 14:03 conectarsistemas

Hi @Gilmar-Conectar!

There's already an ongoing discussion about this. https://externals.io/message/116752

Feel free to send an e-mail with your thoughts to that thread. As this is not a bug but intended behavior I'm closing this issue for now.

iluuu1994 avatar Mar 24 '22 14:03 iluuu1994

I the meantime, what you can do is to make sure you don't handle deprecated messages as fatal errors in your application. That is the purpose of deprecation messages, to flag a behavior that is going to (most probably) transform into an error in a future major version, 9.0 in this case. Having them as errors is basically ignoring their main purpose.

drealecs avatar Mar 24 '22 14:03 drealecs

Please double check your SQL because right now it contains a syntax error. Maybe you have a mistake in your SQL that results in you getting null instead of a string.

kamil-tekiela avatar Mar 24 '22 15:03 kamil-tekiela

Yes, there was a wrong column name in $row, but it was just to clarify that a SUM() can return a NULL, and any function that is used to process a $row, will give an error.

conectarsistemas avatar Mar 24 '22 16:03 conectarsistemas

This is a fundamental over step with strict typing, speaking specifically of trim(). I've been writing php code for 22 years, and this deprecation warning is bringing quite a bit of grief.

If it's null, it still can be a string, trimming it, even when null should not throw a fatal error.

Much of the strict typing introduced was welcome. This one not so much.

The damage is already done, since this code is out of the barn already. It would be good if you could change that behavior though.

TheWitness avatar Sep 08 '23 10:09 TheWitness