php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #79694 Coercion of ints to floats with return types even in strict mode not documented
Submitted: 2020-06-12 01:28 UTC Modified: 2020-06-12 03:34 UTC
From: cxlblm at gmail dot com Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: 7.4.7 OS: macos 10.15.5
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: cxlblm at gmail dot com
New email:
PHP Version: OS:

 

 [2020-06-12 01:28 UTC] cxlblm at gmail dot com
Description:
------------
We can see https://3v4l.org/tmcO4 
What is the type of numeric constant

Test script:
---------------
<?php
declare(strict_types=1);

function testFloat(): float
{
    return 1;
}

var_dump(is_float(testFloat()));
var_dump(is_float(1));


function testInteger(): int
{
    return 1;
}

var_dump(is_int(testInteger()));
var_dump(is_int(1));


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-06-12 03:34 UTC] requinix@php.net
-Summary: Whether a numeric constant needs to be an integer +Summary: Coercion of ints to floats with return types even in strict mode not documented -Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem -Package: *General Issues +Package: Scripting Engine problem
 [2020-06-12 03:34 UTC] requinix@php.net
The type is exactly what it looks like. No tricks.

integers may always be coerced to floats, even with strict_types=1. This is covered in the original RFC but not in the online docs for return types.
https://wiki.php.net/rfc/scalar_type_hints_v5#behaviour_of_strict_type_checks
https://www.php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration

Note that section does mention return typing works like argument typing does, and their docs do mention the int->float exception.
https://www.php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC