On 29 Oct 2023, at 11:14, Kamil Tekiela <tekiela...@gmail.com> wrote:
> A code like this already throws a fatal error.
> 
> function enc(string $a){}
> enc(null);
> 
> The only thing remaining to be fixed in PHP 9 is to make this error 
> consistent on all function invocations.



Or, be consistent with all of the other type coercions?


```
function user_function(string $s, int $i, float $f, bool $b) {
  var_dump($s, $i, $f, $b);
}
 
user_function('1', '1', '1', '1');
  // string(1) "1" / int(1) / float(1) / bool(true)
 
user_function(2, 2, 2, 2);
  // string(1) "2" / int(2) / float(2) / bool(true)
 
user_function(3.3, 3.3, 3.3, 3.3);
  // string(3) "3.3" / int(3), lost precision / float(3.3) / bool(true)
 
user_function(false, false, false, false);
  // string(0) "" / int(0) / float(0) / bool(false)
```


The documentation does clearly say how NULL should be coerced:

https://www.php.net/manual/en/language.types.string.php
"null is always converted to an empty string."

https://www.php.net/manual/en/language.types.integer.php
"null is always converted to zero (0)."

https://www.php.net/manual/en/language.types.float.php
"For values of other types, the conversion is performed by converting the value 
to int first and then to float"

https://www.php.net/manual/en/language.types.boolean.php
"When converting to bool, the following values are considered false [...] the 
special type NULL"


Maybe documentation should be amended to say "except when being passed to a 
function"?

Or, should we have fatal errors with the following as well:

```
$nullable = NULL;

print($nullable);

echo $nullable;

printf('%s', $nullable);

var_dump('A' . $nullable);

var_dump(3 + $nullable);

var_dump($nullable / 6);

var_dump('' == $nullable);
```



> Without it, you would never know that you have a logical error in your code.


But it's not an error?

Craig

Reply via email to