|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-11-14 16:51 UTC] duodraco@php.net
Description: ------------ Following docs (http://php.net/manual/pt_BR/functions.arguments.php#functions.arguments.type-declaration.strict) 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: ---------------- Y 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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 16:00:02 2025 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: https://wiki.php.net/rfc/scalar_type_hints_v5#behaviour_of_weak_type_checks