|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2021-06-19 18:51 UTC] 6562680 at gmail dot com
[2021-06-19 18:54 UTC] rtrtrtrtrt at dfdfdfdf dot dfd
[2021-06-19 19:54 UTC] 6562680 at gmail dot com
[2021-06-19 20:18 UTC] requinix@php.net
-Status: Open
+Status: Not a bug
-Type: Feature/Change Request
+Type: Bug
-Package: Filter related
+Package: *General Issues
[2021-06-19 20:18 UTC] requinix@php.net
[2021-06-19 20:38 UTC] 6562680 at gmail dot com
[2021-06-19 20:55 UTC] a at b dot c
[2021-06-19 21:14 UTC] rtrtrtrtrt at dfdfdfdf dot dfd
[2021-06-19 21:30 UTC] nikic@php.net
-Block user comment: No
+Block user comment: Yes
[2021-06-19 21:30 UTC] nikic@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 14:00:01 2025 UTC |
Description: ------------ Usually it happens when another function pass its result to current, and it causes script to work another way than request. 1) If use just "$a" - it will fail is i didnt pass the argument 2) If use "int $a" - it will fail if argument is not an integer 3) If use "$a = 123" - if wont fail and if i DIDNT PASS argument - it will be 123, but i still can pass NULL and no fail there - seems correct, but logically NULL = undefined there. 4) If i use typehint "int $a = 123" - it will throw error if i pass NULL... What, i want 123 if i pass the null. 5) Ask my devs to ensure strict_types and rewrite all the project. Or just believe that "speakers" (they aren't coders i think) who just cry "use everywhere strict_types", and then solve that damn question marks in types... For string its more complicated - empty string is string too... Thank god we havent one more type = "undefined", in my 10 years practice that stuff was needed maybe... 2-3 times for functions that could return anything/mixed. There's not only manual code, some variable could contain NULL too. Currently i solve it like "section 2" in script textarea below. It allows "undefined" means - passed NULL or undefined, and controls integers. So on Once i had just heared "You should use Classes instead of primitives all the time" and "strong-typed languages better than your php", i dont want to believe, but seems i should... Test script: --------------- // section 1 function hello($a = 123) { var_dump($a); // NULL } $a = null; hello($a); // section 2 function hello(int $a = null) { $a = $a ?? 123; var_dump($a); // 123 }