|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70915 strict_types = 0 affects only string and bool
Submitted: 2015-11-14 16:51 UTC Modified: 2015-11-14 17:01 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: Assigned:
Status: Not a bug Package: *Programming Data Structures
PHP Version: 7.0.0RC7 OS: Linux (4.2.0)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
16 + 35 = ?
Subscribe to this entry?

 [2015-11-14 16:51 UTC]
Following docs ( when we have no declaration of strict_types or it's set to 0 the default behavior for scalar types is to coerce to declared type.

It's ok for string and boolean. When we set a parameter to int or float PHP throws a TypeError - the expected behavior for strict_types = 1.

When strict_types is set to 1 everything runs as expected.

Test script:
declare(strict_types = 0);
function test(int $grade)
    return $grade >= 7 ? 'X' : 'Y';

willInternTurnEmployee(8); //'Pass' and OK

echo willInternTurnEmployee('Really?');
//Expected: 'Fail', string converted to int
//TypeError thrown

Expected result:

Actual result:
Fatal error: Uncaught TypeError: Argument 1 passed to willInternTurnEmployee() must be of the type integer, string given, called in /path/to/Projects/php7-demo/app/x.php on line 12 and defined in /path/to/Projects/php7-demo/app/x.php:4
Stack trace:
#0 /path/to/Projects/php7-demo/app/x.php(12): willInternTurnEmployee('Really?')
#1 {main}
  thrown in /path/to/Projects/php7-demo/app/x.php on line 4


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-14 16:54 UTC]
It affects both 'Scalar Type Hints' and 'Return Type Declarations'
 [2015-11-14 17:01 UTC]
-Status: Open +Status: Not a bug
 [2015-11-14 17:01 UTC]
Weak type hints follow the same rules as zpp ("Works the same way as internal functions" basically). They do not allow arbitrary coercions, just a lot of them. Here's a table documenting the precise semantics:
 [2015-11-14 17:25 UTC]
Agreed @nikic. It would be good to open an issue to documentation updating it with this conversion table from RFC
 [2015-11-14 17:30 UTC]
Sorry. It was a question. Do you think is reasonable to include that table on docs at first link I sent? If yes I can update it.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 14 11:01:29 2024 UTC