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: 2017-04-14 09:18 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:
Status: Analyzed Package: *Math Functions
PHP Version: 7.0Git-2017-04-12 (Git) OS: Ubuntu
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Sep 21 08:01:26 2019 UTC