php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75507 Cast in function
Submitted: 2017-11-09 14:03 UTC Modified: 2017-11-09 14:49 UTC
From: ciloe at hotmail dot fr Assigned:
Status: Not a bug Package: PHP Language Specification
PHP Version: 7.1.11 OS:
Private report: No CVE-ID: None
 [2017-11-09 14:03 UTC] ciloe at hotmail dot fr
Description:
------------
Hi, when you want to set an hard pypage in a function to test if you have a string/integer/float

The result of var_dump will be float(123) and not a typage error !!!

Test script:
---------------
function test (float $element)
{
    var_dump($element);
}

test('123');
die;

Expected result:
----------------
Catchable fatal error: Argument 1 passed to test() must be an instance of float, instance of string given

Actual result:
--------------
float(123)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-11-09 14:19 UTC] sjon at hortensius dot net
This is what strict-types are for - see https://3v4l.org/nUKlM and https://wiki.php.net/rfc/scalar_type_hints_v5
 [2017-11-09 14:49 UTC] pollita@php.net
-Status: Open +Status: Not a bug
 [2017-11-09 14:49 UTC] pollita@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

As sjon at hortensius dot net stated, this is expected behavior for coercive type enforcement.  To enable strict type enforcement, include `declare(strict_types=1);` at the top if your *calling* script.  Note that strict enforcement is performed at the callsite and is set on a per-file basis.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Sep 20 01:03:37 2021 UTC