Bug #74422 [Com]: Round function returns false for non-numeric argument
| From: | puggan at sundragon dot se | Date: | Wed, 30 Oct 2019 23:07:20 +0000 |
| Subject: | Bug #74422 [Com]: Round function returns false for non-numeric argument | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-223526@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=74422&edit=1
ID: 74422
Comment by: puggan at sundragon dot se
Reported by: jose dot carlos dot garcia dot 95 at gmail dot com
Summary: Round function returns false for non-numeric
argument
Status: Analyzed
Type: Bug
Package: *Math Functions
Operating System: Ubuntu
PHP Version: 7.0Git-2017-04-12 (Git)
Block user comment: N
Private report: N
New Comment:
The false as return value is mention for floor([]), but not for ceil([]) or round([]).
If its worth mention on floor, shouldn't it be mention on the others?
(Or should it be removed from floor?)
Previous Comments:
------------------------------------------------------------------------
[2017-04-17 08:44:16] jose dot carlos dot garcia dot 95 at gmail dot com
Yes, make a correction in documentation could be nice too!
But I think is necessary make an advertency that round would return a non-float value.
------------------------------------------------------------------------
[2017-04-14 09:18:25] cmb@php.net
> But, if I try with a string or a boolean value, round return 0
> and not false, so it's strange.
That is because the argument is converted to a number[1] and only
if that fails, FALSE is returned[2]. Changing that to return NULL
is trivial, but would be a BC break for not much gain. Perhaps
it's better to fix the documentation instead.
[1] <https://github.com/php/php-src/blob/php-7.1.4/ext/standard/math.c#L379>
[2] <https://github.com/php/php-src/blob/php-7.1.4/ext/standard/math.c#L396>
------------------------------------------------------------------------
[2017-04-12 20:20:26] jose dot carlos dot garcia dot 95 at gmail dot com
But, if I try with a string or a boolean value, round return 0 and not false, so it's strange.
I think that null value is a good option.
------------------------------------------------------------------------
[2017-04-12 20:15:11] requinix@php.net
Typically built-in functions return NULL when given an argument of the wrong type, but returning
false is also permitted.
http://php.net/manual/en/functions.internal.php
Looks like round RETURN_FALSEs for values that aren't int or float. It should be fine to change
that, right?
------------------------------------------------------------------------
[2017-04-12 17:46:44] jose dot carlos dot garcia dot 95 at gmail dot com
Description:
------------
In round documentation specified that round everytime return a float value, but if I put an array as
argument, round will return a boolean value.
Test script:
---------------
<?php
$valores = [
array(),
array("test" => "hola")
];
foreach($valores as $valor) {
echo var_dump(round($valor));
}
Expected result:
----------------
A float value, or maybe a change in round function that reports when a type is invalid.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74422&edit=1