php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74422 Round function returns false for non-numeric argument
Submitted: 2017-04-12 17:46 UTC Modified: 2019-10-31 10:19 UTC
Votes:2
Avg. Score:3.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: jose dot carlos dot garcia dot 95 at gmail dot com Assigned: nikic (profile)
Status: Closed Package: *Math Functions
PHP Version: 7.0Git-2017-04-12 (Git) OS: Ubuntu
Private report: No CVE-ID: None
 [2017-04-12 17:46 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-04-12 20:15 UTC] requinix@php.net
-Summary: Round function sometimes return no float values +Summary: Round function returns false for non-numeric argument -Status: Open +Status: Analyzed
 [2017-04-12 20:15 UTC] 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 20:20 UTC] 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-14 09:18 UTC] 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-17 08:44 UTC] 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.
 [2019-10-30 23:07 UTC] puggan at sundragon dot se
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?)
 [2019-10-31 10:19 UTC] nikic@php.net
-Status: Analyzed +Status: Closed -Assigned To: +Assigned To: nikic
 [2019-10-31 10:19 UTC] nikic@php.net
round([]) will throw a TypeError in PHP 8. Both false and null will no longer be possible return values.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Dec 05 20:01:24 2019 UTC