# Allowing NULL for some internal functions

- Thread: https://externals.io/message/116752
- Original archive: https://news-web.php.net/php.internals/116752
- Started by Craig Francis on 2022-01-01T23:17:43+00:00
- Messages: 61
- Page 1 of 3: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/116752.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #116752 — Craig Francis — 2022-01-01T23:17:43+00:00

URL: https://externals.io/message/116752#116752

> On Thu, Dec 2, 2021 at 8:48 AM Craig Francis <craig@craigfrancis.co.uk>
> wrote:
>
> > […]
>
> I'm not hard against this idea.  The interpretation of null in these
> contexts as being equivalent to empty string isn't unreasonable.  I guess
> the only objection I could have would be an academic one and I can't really
> defend that.  So yeah, sure... why not?
>


Thanks Sara,

I've spent a few days coming up with a short(ish) list of parameters that
are likely to receive `NULL`.

The focus is on making it easier for developers to upgrade to newer
versions of PHP. I'm thinking of the typical developer, probably using
WordPress, isn't using Psalm (at levels 1 to 3, with no baseline), and
isn't using `strict_types`; where they don't really have the time to add
 `strval()` to everything that could be NULL.

Draft RFC:
https://wiki.php.net/rfc/allow_null

And the list, where I'm proposing we only relax this requirement for the
*bold* parameters:
https://github.com/craigfrancis/php-allow-null-rfc/blob/main/functions-change.md

I'll give it a few weeks to see if there are any parameter suggestions
(welcome via email, pull request, carrier pigeon, etc), then I'll start
looking for the best way to implement this.

Craig


PS; starting new thread, as I have gone a bit off topic, original one at:
https://externals.io/message/116519#116556

---

## #116758 — AllenJB — 2022-01-02T11:26:59+00:00

URL: https://externals.io/message/116752#116758 · In reply to: #116752

> I've spent a few days coming up with a short(ish) list of parameters that
> are likely to receive `NULL`.
>
> The focus is on making it easier for developers to upgrade to newer
> versions of PHP. I'm thinking of the typical developer, probably using
> WordPress, isn't using Psalm (at levels 1 to 3, with no baseline), and
> isn't using `strict_types`; where they don't really have the time to add
> `strval()` to everything that could be NULL.
>
> Draft RFC:
> https://wiki.php.net/rfc/allow_null
>
> And the list, where I'm proposing we only relax this requirement for the
> *bold* parameters:
> https://github.com/craigfrancis/php-allow-null-rfc/blob/main/functions-change.md
>
> I'll give it a few weeks to see if there are any parameter suggestions
> (welcome via email, pull request, carrier pigeon, etc), then I'll start
> looking for the best way to implement this.
>
> Craig
>
>
> PS; starting new thread, as I have gone a bit off topic, original one at:
> https://externals.io/message/116519#116556

This RFC has come out of a similar discussion around deprecating dynamic 
properties (and this isn't the first time "upgrading is hard" / "the 
language is moving too fast" has come up), so it's not just this one 
change that's affected.

I think this RFC is trying to solve the wrong problem / only fixing one 
small portion of the "immediate" problem without paying attention to the 
bigger picture.


Alternative solutions:

I feel that effort would be better spent on improving and encouraging 
use of tooling such as the PHPCompatibility CodeSniffer ruleset (which 
has unfortunately fallen behind for changes in PHP 8+) and Rector to 
improve the upgrading process for everyone. I recognize that there's 
arguments against the project being seen to endorse / contribute to 
specific tools / libraries / frameworks, but I feel that in this area 
the benefits outweigh the costs.

The project could provide guides (or link to selected existing articles) 
for upgrading older code. Whilst significant documentation does already 
exist on this it's not always easy to find or contains errors (a lot of 
- probably copy-paste - blog articles that I've seen focus on 
PHPCompatibility for upgrading to 8.0 / 8.1 even tho its coverage of the 
changes in these versions is poor). This could be done via something 
like a user-contributed wiki similar to what Python and other projects 
do: https://wiki.python.org/moin/FrontPage - I recognize that there's 
significant work in this suggestion, but I feel it also offers good 
opportunities for further improvements to the PHP ecosystem / community.

The migration guides could include documentation on updating existing 
code for each change (could / should more be done to encourage this 
being done at the time the change is made rather than trying to compile 
migration docs just before release?) - they do this sometimes but I feel 
this is an area with room for improvement.

The above mentioned alternatives solve the entire class of problem 
whilst also maintaining PHP's work towards being a stricter language 
that's that much harder to create bugs with and needs less "boilerplate" 
for more defensive programming. Which leads to the argument that in 
trying to solve a one-time problem for some developers, this RFC creates 
work for developers both in debugging code, and for developers with 
defensive programming styles to (re-)add checks to detect the exact 
class of problem these deprecations / warnings highlight.

Developers also already have tools to selectively silence deprecation 
notices using custom error handlers.


On the proposed changes / RFC content:

The RFC uses retrieving request parameters (get/post/cookie) values 
using various frameworks as a (primary) example. With the exception of 
CodeIgniter (it may be possible, but wasn't immediately obvious from the 
quickest of glances at their docs) all the frameworks used in the 
example have an additional default value parameter. This makes all of 
these (and the null coalesce alternative) relatively trivial to update 
using a regular expression find and replace, that most text editors can do.

The RFC focuses on changing string parameters, but uses 
 `mysqli_fetch_row()`, `json_decode()` and `error_get_last()` in its arguments 
- none of which are usually used to return a string (or anything you'd 
want to treat as a string). Accessing properties of non-objects (since 
PHP 5.0) and accessing non-existant array indexes (since PHP 7.4) have 
been warnings / notices for a signifcant time already.

Some of the suggested functions (parameters) to change are strange 
selections in my opinion - gz functions, bcmath parameters, 
 `password_hash()` and `mail()` (subject / message) are not usually values 
you want to pass either null or an empty value to (and are almost 
certainly bugs if the code does that). (And similarly from the maybe 
list, I would apply this to most of the date_create functions, dns 
functions hostname and logging functions message parameter)

If this change were to be made, I think it would be better to severely 
restrict the list to only the most commonly used functions. How many 
codebases affected by the null parameter deprecation are using sodium or 
MessageFormatter? While this might leave some work for developers 
upgrading codebases, I feel the long list of functions affected by this 
RFC (even without considering the maybe list) goes too far.

How would code that declares strict_types be affected by this RFC? This 
isn't clearly defined in the RFC. I would prefer it if these function 
continue to not accept null (and throw a TypeError) if strict_types is 
declared.

---

## #116760 — Christoph M. Becker — 2022-01-02T13:41:15+00:00

URL: https://externals.io/message/116752#116760 · In reply to: #116752

> 
>
> > […]
>
> I've spent a few days coming up with a short(ish) list of parameters that
> are likely to receive `NULL`.
>
> The focus is on making it easier for developers to upgrade to newer
> versions of PHP. I'm thinking of the typical developer, probably using
> WordPress, isn't using Psalm (at levels 1 to 3, with no baseline), and
> isn't using `strict_types`; where they don't really have the time to add
> `strval()` to everything that could be NULL.
>
> Draft RFC:
> https://wiki.php.net/rfc/allow_null
>
> And the list, where I'm proposing we only relax this requirement for the
> *bold* parameters:
> https://github.com/craigfrancis/php-allow-null-rfc/blob/main/functions-change.md
>
> I'll give it a few weeks to see if there are any parameter suggestions
> (welcome via email, pull request, carrier pigeon, etc), then I'll start
> looking for the best way to implement this.

In my opinion, it makes no sense to special case some functions in this
regard; the selection of which parameters of which function to special
case will always be too long for some, and to short for others.  And
even if we find an acceptable compromise, what about functions of
external extensions (e.g. PECL packages, but also others, maybe private
packages).  We have not much, if any, say on these, but they will be
affected by the deprecation anyway.

And then it's totally unclear to me how this is supposed to affect
strict_types=1.  Either the respective string parameters are made
nullable unconditionally, so null would be accepted in strict mode as
well, what doesn't appear to be favorable.  Or do we want to special
case this kind of nullable parameters, so they are only nullable for
strict_types=0?  In my opinion, that would be the worst possible
outcome.  How should this even be documented?

If the BC break is deemed to serious (maybe for string parameters only),
we better consider to undeprecate this, although that would make
internal functions behave differently to userland functions for
strict_types=0.  But frankly, in that case the original RFC[1] should
not have passed, but it did, with 46 votes in favor, and *none* against.

[1] <https://wiki.php.net/rfc/deprecate_null_to_scalar_internal_arg>

Christoph

---

## #116762 — Andreas Leathley — 2022-01-02T14:22:45+00:00

URL: https://externals.io/message/116752#116762 · In reply to: #116752

> I've spent a few days coming up with a short(ish) list of parameters that
> are likely to receive `NULL`.
>
> The focus is on making it easier for developers to upgrade to newer
> versions of PHP. I'm thinking of the typical developer, probably using
> WordPress, isn't using Psalm (at levels 1 to 3, with no baseline), and
> isn't using `strict_types`; where they don't really have the time to add
> `strval()` to everything that could be NULL.

I don't think the current deprecation notices are a real hindrance in
upgrading to a newer PHP version - there have been many such changes
which require possibly many small adaptions in code in the past, like
making undefined array keys a warning in PHP 8.0 (which seems a lot more
radical than adding the current deprecation notices). Singling out this
specific deprecation/change as impactful seems a bit overblown.

I also feel like such an un-deprecation would be premature. PHP 8.1 has
only just come out, the road until PHP 9.0 is still quite long, and
deprecation notices can easily be ignored for now, for those who do not
want to change their code right away. If in a year or two this is a big
problem in real code, then there would at least be a basis for changing
it, but I suspect most codebases will see more benefit in handling their
types more properly than if the language "sometimes" automatically
converts null to an empty string without complaining about it.

I did have some occurances of this deprecation notice in my applications
when upgrading to 8.1, and each time it was clearly a bug or oversight.
Removing the deprecation notices would also remove those insights into
possibly unreliable code.

---

## #116764 — Craig Francis — 2022-01-02T17:53:46+00:00

URL: https://externals.io/message/116752#116764 · In reply to: #116758

Thanks Allen,

Before I start, the RFC is still a Draft, and I'm trying to find a
solution... I'll also use a bit of sarcasm, just to be entertaining/funny,
so it's not a page of boring technical stuff, I'm not arguing or saying
you're wrong, and I genuinely appreciate your views on this.





> I think this RFC is trying to solve the wrong problem / only fixing one
> small portion of the "immediate" problem without paying attention to the
> bigger picture.
>


Dynamic properties do cause problems, and I'm convinced they should
deprecated, whereas `NULL` is a useful value.

e.g. when a value has not been set; and then providing `NULL` to a function
like `htmlspecialchars()`, I don't think should be a problem.



I feel that effort would be better spent on improving and encouraging
> use of tooling such as the PHPCompatibility CodeSniffer ruleset.



This one requires variable tracing from source to sink, not an easy task
(it's why I noted "Psalm at level 3, with no baseline").

Neither PHPCompatibility or Rector have rules for this at the moment, and
I'm pretty sure they never will; other than the really obvious ones, e.g.
`str_replace('a', NULL, $name)`, which is clearly a terrible thing to do :-)

As an aside, PHPCompatibility did the bad thing of using `trim()` on
getConfigData(), which can return NULL:

https://github.com/PHPCompatibility/PHPCompatibility/commit/ab7b0b82d95c82e62f9cffb0f1960f3ccbf0805e

https://github.com/squizlabs/PHP_CodeSniffer/blob/d6a58bdd8707bab1032344f33d01c0627e0f3f97/src/Config.php#L1479

As noted in the RFC, while these don't take long to fix, finding them is
tricky, it nearly always results in more code (yay?), and it is being seen
as wasting time on a fairly questionable improvement.



The migration guides could include documentation on updating existing
> code for each change



While migration guides are useful (I appreciate them), I'm focusing on this
change to avoid unnecessary work. I don't think nullable variables should
need to be explicitly cast to a string before calling these functions -
doing so doesen't seem to make the code better (if anything, it's making it
more complex).



... this RFC creates work for developers both in debugging code, and for
> developers with
> defensive programming styles to (re-)add checks to detect the exact
> class of problem these deprecations / warnings highlight.
>


For example?

e.g. `$name`, if it's set to NULL, why should `ucwords()` have a problem
with that? Won't it cause extra work having to explicitly convert it to a
string?

Consider those who aren't using Static Analysis (or they are using the
entry levels, with a baseline), it requires that everyone remembers that
while their HTML form would normally provide this value, the user can edit
the <form> in their browser DOM, or a browser extension could do something
weird (e.g. a password manager), or a network issue results in a partial
page load... and kaboom, future Fatal TypeError in the middle of
processing? yay? party time?



...all the frameworks used in the example have an additional
> default value parameter. This makes all of these (and the null
> coalesce alternative) relatively trivial to update using a regular
> expression find and replace, that most text editors can do.



They use a default value of `NULL` so you can determine when the value has
not been provided... should we tell them to stop doing this? default to
returning an empty string instead? I'm not sure they will accept that one.



Some of the suggested functions (parameters) to change are strange
> selections in my opinion



It's a draft, I just started with as many candidates as I could find, on
the basis that it's easier to cut back if `NULL` can cause a problem.

But as to your examples (gz compressing, bcmath, password_hash, and mail);
they can all be provided with user input, and other sources of NULL.

e.g. `password_hash()`, while I wouldn't advise anyone to use a blank
password, or anything less than 8 characters, this function does accept it.
And because passwords often come from a POST request, if the value is not
present (framework function returns NULL)... well, assuming this
deprecation indicates a future where this is not acceptable, what happens?
future Fatal TypeError?

This is why I have the "Future Scope" section - noting that some parameters
could be updated (in a different RFC) to complain when an Empty String or
 `NULL` is provided. e.g. `setcookie()` already complains when `$name` is
empty, and `strpos()` probably should complain when `$needle` is empty.




> If this change were to be made, I think it would be better to severely
> restrict the list to only the most commonly used functions.



I'm fine with that, but what should be removed? Feel free to do this on or
off-list, pull request, etc. If you can, I'd really appreciate examples
where the `NULL` would cause a problem but an empty string is fine - i.e.
assume developers will be lazy, and just use `strval()` to fix the hideous
crime they have been committing.



How would code that declares strict_types be affected by this RFC?



Good question... personally I think a `trim()` on `NULL` should return an empty
string, not a fatal error, so maybe we are looking at this problem in a
slightly different way?

Craig

---

## #116765 — Craig Francis — 2022-01-02T17:54:30+00:00

URL: https://externals.io/message/116752#116765 · In reply to: #116760

> [...]
> And then it's totally unclear to me how this is supposed to affect
> strict_types=1.
> [...]
> If the BC break is deemed to serious (maybe for string parameters only),
> we better consider to undeprecate this, although that would make
> internal functions behave differently to userland functions for
> strict_types=0.  But frankly, in that case the original RFC[1] should
> not have passed, but it did, with 46 votes in favor, and *none* against.
>


I'm open to suggestions on how this should be handled, my RFC is just a
draft.

As to the original RFC, there wasn't really a discussion on it:

https://externals.io/message/112327

The only person who raised a concern was Craig Duncan.

And tbh, just scanning the RFC text, it's very reasonable, as consistency
is ideal... but I didn't realise how much code would need to be changed
until a few projects I work on started trying to use 8.1 (one team has gone
back to 8.0, although I'm hoping they will accept a custom error handler to
ignore these deprecation notices).

Craig

---

## #116766 — Craig Francis — 2022-01-02T17:55:25+00:00

URL: https://externals.io/message/116752#116766 · In reply to: #116762

> I don't think the current deprecation notices are a real hindrance in
> upgrading to a newer PHP version.



Nice, but I suspect your code doesen't exactly represents most code :-)

And while these may be "small adaptions", I work with some people where the
quantity is, impressive... and tbh I can't actually explain what the point
for this change is.

As an aside, it is making it even harder for me to get them to use static
analysis, as they keep saying this stuff is elitist bull... I'll leave it
there :-)


I did have some occurances of this deprecation notice in my applications
> when upgrading to 8.1, and each time it was clearly a bug or oversight.
> Removing the deprecation notices would also remove those insights into
> possibly unreliable code.
>


Would you mind sharing some of them, as I've not seen anything like that
myself (that's genuine interest).

Craig

---

## #116987 — Craig Francis — 2022-02-07T01:27:53+00:00

URL: https://externals.io/message/116752#116987 · In reply to: #116752

> Draft RFC:
> https://wiki.php.net/rfc/allow_null
>


I know, I'm a glutton for punishment, but I've started writing a patch for
this:

https://github.com/craigfrancis/php-src/compare/master...allow-null

It does not affect any Fatal Error exceptions for scripts using
'strict_types'.

For now it simply defines `Z_PARAM_STR_ALLOW_NULL`; it's not used by any
functions yet (that's the next step).

It works a bit like `Z_PARAM_STR_OR_NULL` (but it will return an empty
string instead of NULL).

It should be a fairly easy drop in replacement for `Z_PARAM_STR`.

e.g. To update `htmlspecialchars()`, and related functions, I'd simply
update this line:

https://github.com/php/php-src/blob/7b90ebeb3f954123915f6d62fb7b2cd3fdf3c6ec/ext/standard/html.c#L1324

Please note I'm not a C programmer, and I barely understand the code base,
so constructive/friendly feedback is welcome.

Thanks,
Craig



Extra notes...

No-one has given a single reason why any one of the listed functions should
not allow `NULL`; that said, I appreciate that developers using
`strict_types` do prefer this (and they will not be affected by this).

I've not seen any suggestion on how Rector, PHPCompatibility, or anything
else could provide a way to fix this issue automatically.

Only very strict static analysis (e.g. Psalm at levels 1 to 3, with no
baseline) currently picks up this issue.

I am simply trying to "relax certain type requirements on a case by case
basis" (which will help upgrading to PHP 9); ref the original discussion:

https://externals.io/message/112327

I know one person simply said this was a "terrible idea", but I'm still
waiting to hear any details on why.

In response to the "it's a bit late" position; we only have a deprecation
at the moment (can be ignored), it will be too late when PHP 9 starts
raising Fatal Error exceptions for anyone committing the heinous crime of
passing `NULL` to these functions (for reference, WordPress aren't exactly
rushing to fix this one).

The change in behaviour is done in `zend_parse_arg_str_weak()`, so I don't
think it will make much of a performance difference; but considering how
much effort went in to testing `is_literal()`, and how people still managed
to mis-read that, and how other RFCs don't mention performance *details* at
all, I'm skipping that bit of pain.

And yes, I have seen the -4 on externals.io... thank you for that
</sarcasm>.

---

## #116988 — Mark Randall — 2022-02-07T08:35:54+00:00

URL: https://externals.io/message/116752#116988 · In reply to: #116987

> I know one person simply said this was a "terribl > idea", but I'm still
> waiting to hear any details on why.

The changes you propose are not something that I am comfortable with 
either.

I understand your motivations in proposing them, but to my mind it goes 
against the direction that PHP is developing, which I think is the right 
one, where errors and likely errors result in stopping execution rather 
than allowing it continue, potentially silently (dependent on error 
handling settings).

If a parameter expects a string, that is what it should be given, and 
its the callers' responsibility to ensure that is the case. If they fail 
to do so then it's an error just like any other.

IMHO reverting to "If it's a null we'll just pretend its a string" is 
contrary to how the language should be progressing.

It sucks that it was ever allowed in the first place.

PHP has a long history of making descisions to try to make things 'just 
work', and if history teaches us anything, its that we inevitably come 
to regret these descisions down the line.

Mark Randall

---

## #116989 — Lynn — 2022-02-07T08:52:54+00:00

URL: https://externals.io/message/116752#116989

> 
> > […]
>
> The changes you propose are not something that I am comfortable with
> either.
>
> I understand your motivations in proposing them, but to my mind it goes
> against the direction that PHP is developing, which I think is the right
> one, where errors and likely errors result in stopping execution rather
> than allowing it continue, potentially silently (dependent on error
> handling settings).
>
> If a parameter expects a string, that is what it should be given, and
> its the callers' responsibility to ensure that is the case. If they fail
> to do so then it's an error just like any other.
>
> IMHO reverting to "If it's a null we'll just pretend its a string" is
> contrary to how the language should be progressing.
>
> It sucks that it was ever allowed in the first place.
>
> PHP has a long history of making descisions to try to make things 'just
> work', and if history teaches us anything, its that we inevitably come
> to regret these descisions down the line.
>

I agree with this sentiment. I rather see solutions that solve null values
being passed to these functions instead. The in-house legacy framework I
use has a small array wrapper that through `ArrayAccess` ensures `null` is
returned instead of a notice/warning being thrown when accessing a key that
does not exist. During `offsetGet` it will return itself wrapped around the
value if it's an array. I'm not saying that this is a good alternative for
the problem, it's just one of the solutions that can be taken to tackle
issues with `$_POST` and `$_GET` access for example.

---

## #116990 — Christian Schneider — 2022-02-07T09:34:38+00:00

URL: https://externals.io/message/116752#116990

Am 07.02.2022 um 09:35 schrieb Mark Randall <marandall@php.net>:
> 
> > […]
> 
> I understand your motivations in proposing them, but to my mind it goes against the direction that PHP is developing, which I think is the right one, where errors and likely errors result in stopping execution rather than allowing it continue, potentially silently (dependent on error handling settings).

I think some proposals for PHP are going too far with strictness so I very much like Crag's RFC.

> If a parameter expects a string, that is what it should be given, and its the callers' responsibility to ensure that is the case. If they fail to do so then it's an error just like any other.

The decision to define e.g. `strlen()` as strlen(string $string) instead of strlen(?string $string) was arbitrary and I for one would prefer the loose definition.

I'm confident that any real world example would abort at an early enough stage to catch errors in your program, without something as basic as `strlen()` having to do the job while adding complexity for something as simple as "if (strlen($x)) ..".

> IMHO reverting to "If it's a null we'll just pretend its a string" is contrary to how the language should be progressing.
> It sucks that it was ever allowed in the first place.

I think we agree to disagree here ;-)

> PHP has a long history of making descisions to try to make things 'just work', and if history teaches us anything, its that we inevitably come to regret these descisions down the line.


PHP has a long history of being a concise, pragmatic language and there are many who do not regret this at all.
Finding the right balance is key here.

- Chris

---

## #116991 — Craig Francis — 2022-02-07T12:05:00+00:00

URL: https://externals.io/message/116752#116991

> I understand your motivations in proposing them, but to my mind it goes
> against the direction that PHP is developing, which I think is the right
> one, where errors and likely errors result in stopping execution rather
> than allowing it continue
>


Hi Mark,

Thanks for responding.

I'm not completely against this approach in general, but I'm trying to find
the right balance.

Back to my example; most frameworks return `NULL` when a user value is not
provided via GET/POST/COOKIE (this is a useful feature), and many
developers simply pass that nullable string through to functions like
 `htmlspecialchars()`, `urlencode()`, etc... I do not understand why this is an
error so bad that it must result in a Fatal Error for everyone using PHP 9.

As noted before, I can see how `NULL` might represent a problem in projects
using `strict_types`, but I'm not proposing we change anything for them
(also much more likely to be using static analysis).

And specifically with NULL, strings in C are a "one-dimensional array of
characters terminated by a null character", so you could say that `NULL` is
very similar to an empty string - maybe that helps address the "pretend its
a string" objection?

Craig

---

## #116992 — G. P. B. — 2022-02-07T12:07:24+00:00

URL: https://externals.io/message/116752#116992 · In reply to: #116990

On Mon, 7 Feb 2022 at 09:34, Christian Schneider <cschneid@cschneid.com>
wrote:

> > […]
> its the callers' responsibility to ensure that is the case. If they fail to
> do so then it's an error just like any other.
>
> The decision to define e.g. `strlen()` as strlen(string $string) instead of
> strlen(?string $string) was arbitrary and I for one would prefer the loose
> definition.
>
> - Chris
>

I'm not sure what you are getting on here? `strlen()` was always defined to
only take a string and never was nullable in the first place.

The RFC which is at the heart of this is the one to make internal functions
and userland functions consistent.
Therefore introducing a mechanism which makes only *some* functions special
and weird is a complete no go for me.
Either there is a (strong) case for the parameter to be nullable, and then
it should be marked as such and not do weird shenanigans with regards to
strict_types (which I dislike more and more by the second), or there is not.

The list of proposed functions is also excessive and just feels that any
function which raised a diagnostic during a test run on 8.1 was chugged
into it.
Let's look at an example taken from the document (
https://github.com/craigfrancis/php-allow-null-rfc/blob/main/functions-change.md#output
)
- setcookie(name:string, *value:string*, expires_or_options:array|int,
*path:string*, *domain:string*, secure:bool, httponly:bool)
- setrawcookie(name:string, *value:string*, expires_or_options:array|int,
*path:string*, *domain:string*, secure:bool, httponly:bool)

I really do not see why for the path and the domain should *ever* accept
NULL, as if somehow that happens I would have some serious questions about
how you get that information.
However, there *might* be an argument to be made to make the value
parameter nullable. Although I would find this instance very questionable.

I'm not against in principle of marking certain parameters as nullable, but
it for sure should not be on a basis of using strict_types or not.

Best regards,

George P. Banyard

---

## #116993 — Mark Randall — 2022-02-07T12:34:04+00:00

URL: https://externals.io/message/116752#116993 · In reply to: #116991

> And specifically with NULL, strings in C are a "one-dimensional array of
> characters terminated by a null character", so you could say that `NULL` is
> very similar to an empty string - maybe that helps address the "pretend its
> a string" objection?


In this case we're talking about the difference between a char* that 
points to a 1 byte buffer containing '\0' and a char* that itself points 
to NULL. Trying to perform string operations on a null pointer in C 
would equally blow up.

With regards to your framework comment, any framework worth its salt 
will also have the option to provide a default value other than null 
when the value is not provided.

It is the responsibility of the user to make use of this functionality 
to provide the correct type for what the soon-to-be-called function is 
expecting.

Mark Randall

---

## #116995 — Craig Francis — 2022-02-07T12:39:38+00:00

URL: https://externals.io/message/116752#116995 · In reply to: #116992

> The list of proposed functions is also excessive and just feels that any
> function which raised a diagnostic during a test run on 8.1 was chugged
> into it.
>


Not really, while I did create a throw-away script that used
`get_defined_functions()` and `ReflectionFunction` to find all function
arguments that accepted strings, and not `->allowsNull()`... I manually
checked every single one, and I considered if passing `NULL` to that argument
could represent a problem that warrants a Fatal Error for *everyone*.

https://github.com/craigfrancis/php-allow-null-rfc/blob/main/functions.php

This is why I have 2 other lists which I don't think justify any changes;
e.g. it's almost certainly wrong to pass `NULL` to the `$characters`
argument in `trim()`:

https://github.com/craigfrancis/php-allow-null-rfc/blob/main/functions-maybe.md
https://github.com/craigfrancis/php-allow-null-rfc/blob/main/functions-other.md

I'd argue that the length of the list represents the size of the problem
(i.e. the amount of changes developers will need to make for PHP 9)... for
example...


- setcookie
> - setrawcookie
>
> I really do not see why for the path and the domain should *ever* accept
> NULL.


Developers will often use `NULL` for argument defaults (remember, we didn't
show that the default was an empty string in the documentation, just that
it was an optional argument)... and considering the output for these two
functions, I see it as pretty reasonable that `NULL` was used as a way to say
"do not set the path attribute".

    setcookie('name', 'value', 0, '/', '', true); // set-cookie: a=a;
path=/; secure
    setcookie('name', 'value', 0, '', '', true); // set-cookie: a=a; secure
    setcookie('name', 'value', 0, NULL, '', true); // set-cookie: a=a;
secure

So back to the same question as before, why should this `NULL` in the last
line result in a Fatal Error for everyone who does this in PHP 9?

Craig

---

## #116996 — Christian Schneider — 2022-02-07T12:57:37+00:00

URL: https://externals.io/message/116752#116996 · In reply to: #116992

Am 07.02.2022 um 13:07 schrieb G. P. B. <george.banyard@gmail.com>:
> 
> > […]
> 
> The decision to define e.g. `strlen()` as strlen(string $string) instead of strlen(?string $string) was arbitrary and I for one would prefer the loose definition.
> 
> - Chris
> 
> I'm not sure what you are getting on here? `strlen()` was always defined to only take a string and never was nullable in the first place.

Now this is a little bit misleading: Before 7.0 we did not have scalar type hints so the documentation of strlen(string $string) was somewhat lying, especially because back in those days the automatic type jugging from null to "" was used a lot.

> The RFC which is at the heart of this is the one to make internal functions and userland functions consistent.
> Therefore introducing a mechanism which makes only *some* functions special and weird is a complete no go for me.
> Either there is a (strong) case for the parameter to be nullable, and then it should be marked as such and not do weird shenanigans with regards to strict_types (which I dislike more and more by the second), or there is not.

So you are basically saying that we should discuss making it
	strlen(?string $string): int
which would be cleaner, I agree.

About the case having to be strong: I could also argue that you have to make a strong case on why null should not be allowed any more (any more as per implementation, not documentation) as it is a seizable BC.

There are other things which can go wrong with `strlen()` on a higher level (e.g. misuse with utf-8) and I would like to see a real world code where strlen(null) actually broke something while not generating any other error in the code path.

- Chris

---

## #116997 — Larry Garfield — 2022-02-07T15:14:39+00:00

URL: https://externals.io/message/116752#116997 · In reply to: #116996

> Am 07.02.2022 um 13:07 schrieb G. P. B. <george.banyard@gmail.com>:

> So you are basically saying that we should discuss making it
> 	strlen(?string $string): int
> which would be cleaner, I agree.

I would agree here as well.  The goal is consistency, so keep it consistent.  If there's an argument to be made to rewiden a type to include null, make that case and have that debate on a function by function basis.  No mode-based switcheroos.

--Larry Garfield

---

## #116998 — Craig Francis — 2022-02-07T15:34:44+00:00

URL: https://externals.io/message/116752#116998

> With regards to your framework comment, any framework worth its salt
> will also have the option to provide a default value other than null
> when the value is not provided.
>


Frameworks do allow developers to provide their own default value when a
GET/POST/COOKIE value has not been provided by the HTTP request.

But the 4 frameworks I've noted have already chosen to return `NULL` by
default... and while the distinction between an empty string and `NULL` is
rarely used, it can be helpful.

The only oddity I'm aware of is WordPress (who don't really care about
types), and while `get_query_var()` doesn't exactly map to `$_REQUEST`,
they did decide to use an empty string for their `$default`:

https://developer.wordpress.org/reference/functions/get_query_var/

So are you suggesting that all the frameworks should change their default
to an empty string? or every single project should update every use of
these input functions to always override this default, or cast the received
value to always be a string? all to avoid a Fatal Error if a `NULL` value
ever dared to be passed to `htmlspecialchars()` and similar functions in
PHP 9?

Or would it be simpler to allow `NULL` for some function arguments?

---

## #117000 — Craig Francis — 2022-02-07T16:39:42+00:00

URL: https://externals.io/message/116752#117000 · In reply to: #116997

>  The goal is consistency, so keep it consistent.



I'd be happy with that.

The only reason I've taken this approach is for developers using type
checking to identify certain mistakes in their code (which can be useful);
so I wanted to keep them happy by not changing any of the stricter "string
only" arguments... that's why my patch silently makes PHP a bit more
tolerant of `NULL` for the projects that aren't using `strict_types`... and,
as noted before, I'm not suggesting all arguments - e.g. `NULL` is quite
likely a bug for the `$characters` argument in `trim()`, and PHP probably
should complain, even though an empty string does not.


If there's an argument to be made to rewiden a type to include null, make
> that case and have that debate on a function by function basis.



I'm fine with that, but I worry people are underestimating the size of the
problem (having a debate on each one would take a long time).

I've put together my proposed list, it took a few days, and a lot of
thought:

https://github.com/craigfrancis/php-allow-null-rfc/blob/main/functions-change.md

I've asked for suggestions and pull requests a few times, and the best I've
got is `$path`/`$domain` for `setcookie()` (I disagree because `NULL` is
often used for those arguments, so that `$secure` and `$httponly` can be
set to true).

At this point I think people should be suggesting why any of these function
arguments should trigger a Fatal Error in PHP 9 if they receive NULL..
again, happy to discuss here; or off-list via email, video call, etc
(thankfully, for my own mental health, the off-list messages have been
positive)... or you can make suggestions on the repo:

https://github.com/craigfrancis/php-allow-null-rfc/issues

https://github.com/craigfrancis/php-allow-null-rfc/pulls

---

## #117002 — Mark Randall — 2022-02-07T17:05:25+00:00

URL: https://externals.io/message/116752#117002 · In reply to: #116998

> So are you suggesting that all the frameworks should change their default
> to an empty string? or every single project should update every use of
> these input functions to always override this default, or cast the received
> value to always be a string? all to avoid a Fatal Error if a `NULL` value
> ever dared to be passed to `htmlspecialchars()` and similar functions in
> PHP 9?


I'm suggesting that every project should be specifying the default they 
want when they call the function. If by omission you call a function in 
a way that effectively says 'give me a null', then don't be surprised if 
it breaks when it gives you what you asked for and you pass it to 
something that wasn't designed for it.

Special casing things is almost always the wrong approach and will lead 
to pain in the long run.

Like "0" being the only non-empty string that's falsey.

---

## #117003 — Larry Garfield — 2022-02-07T18:38:44+00:00

URL: https://externals.io/message/116752#117003 · In reply to: #117000

> 

> If there's an argument to be made to rewiden a type to include null, make
> > […]
>
>
> I'm fine with that, but I worry people are underestimating the size of the
> problem (having a debate on each one would take a long time).
>
> I've put together my proposed list, it took a few days, and a lot of
> thought:
>
> https://github.com/craigfrancis/php-allow-null-rfc/blob/main/functions-change.md

I'm not suggesting a separate RFC for each.  One RFC with several votes in it is fine, IMO.

What I disagree with is changing the definition of strict_types=0 from "use the well-documented coercion logic at parameter and return points" to "use the well-documented parameter and return points, and also add implicit nullability but only for internal functions, sometimes".

If it makes sense for `strlen()` to be nullable, then it makes sense for `strlen()` to be nullable.  One or the other, not both based on a flag.

> I've asked for suggestions and pull requests a few times, and the best I've
> got is `$path`/`$domain` for `setcookie()` (I disagree because `NULL` is
> often used for those arguments, so that `$secure` and `$httponly` can be
> set to true).

Such is the RFC process...

--Larry Garfield

---

## #117004 — Craig Francis — 2022-02-07T19:20:59+00:00

URL: https://externals.io/message/116752#117004 · In reply to: #117003

> I'm not suggesting a separate RFC for each.  One RFC with several votes in
> it is fine, IMO.
>
> What I disagree with is changing the definition of strict_types=0 from
> "use the well-documented coercion logic at parameter and return points" to
> "use the well-documented parameter and return points, and also add implicit
> nullability but only for internal functions, sometimes".
>


Thanks Larry, that's fair... I can do that, and update the documentation as
well (will wait a bit to see if anyone else has any feedback).

Now the next problem, if we are going to have "One RFC with several
votes"... I've got 276 functions (336 arguments) which I think should
preserve their old behaviour (as in, there is no reason why a Fatal Error
should happen with a `NULL` value in PHP 9).

It might be possible to group them, like the 60 functions just related to
encoding values... because voting for them all individually in an RFC would
be a pain (I should know, I was checking each one).

Or should I setup a survey to make it a bit easier to collect yes/no
feedback before the actual RFC vote? I could add a feature to tick or
untick all by default.

Craig


As a side note, there are 99 functions on the "probably not" list (aka
maybe)... and 442 on the "other" list (the edge cases, like `$method` in
`method_exists()`, that would be weird if it was given a NULL).

---

## #117006 — Björn Larsson via internals — 2022-02-07T22:24:17+00:00

URL: https://externals.io/message/116752#117006 · In reply to: #116752

Den 2022-01-02 kl. 00:17, skrev Craig Francis:
> 
> 
> > […]
> 
> 
> Thanks Sara,
> 
> I've spent a few days coming up with a short(ish) list of parameters that
> are likely to receive `NULL`.
> 
> The focus is on making it easier for developers to upgrade to newer
> versions of PHP. I'm thinking of the typical developer, probably using
> WordPress, isn't using Psalm (at levels 1 to 3, with no baseline), and
> isn't using `strict_types`; where they don't really have the time to add
> `strval()` to everything that could be NULL.
> 
> Draft RFC:
> https://wiki.php.net/rfc/allow_null
> 
> And the list, where I'm proposing we only relax this requirement for the
> *bold* parameters:
> https://github.com/craigfrancis/php-allow-null-rfc/blob/main/functions-change.md
> 
> I'll give it a few weeks to see if there are any parameter suggestions
> (welcome via email, pull request, carrier pigeon, etc), then I'll start
> looking for the best way to implement this.
> 
> Craig
> 
> 
> PS; starting new thread, as I have gone a bit off topic, original one at:
> https://externals.io/message/116519#116556
> 
Hi,

Thanks for this RFC! I think it could interesting to see how libraries
that needs to adapt to this are doing it IRL.

One example I have seen how to fix this for e.g. substr is to replace:
$result = substr($string, 0, $length)
with:
$result = substr($string ?? '', 0, $lenght)

So the null coalescing operator comes in quite handy ;-) Now if this is
a good way to solve it is another matter. But if you are a maintainer of
a library that works fine and wants to support PHP 8.1 this will solve
the upgrade in a smooth way.

Would be interesting to see how common this approach is for solving
PHP 8.1 upgrades for non-nullable arguments with internal functions?

I mean, it shows the value with the RFC from a slightly another angle.

Regards //Björn

---

## #117080 — Craig Francis — 2022-02-20T20:38:17+00:00

URL: https://externals.io/message/116752#117080 · In reply to: #116752

> Draft RFC:
> https://wiki.php.net/rfc/allow_null
>


I've been over-complicating this... why should `NULL` be treated so
differently?

<?php
  $values = ['a', 1, 2.3, false, NULL];
  foreach ($values as $value) {
    echo urlencode($value);
  }
?>

It just struck me, this whole thing is about type coercion when not using
`strict_types=1`; and the 8.1 change has introduced an inconstancy by
deprecating the coercion of NULL, a frequently used value.

Craig

---

## #117085 — Christoph M. Becker — 2022-02-21T09:04:16+00:00

URL: https://externals.io/message/116752#117085 · In reply to: #117080

> 
>
> > […]
>
>
> I've been over-complicating this... why should `NULL` be treated so
> differently?
>
> <?php
>   $values = ['a', 1, 2.3, false, NULL];
>   foreach ($values as $value) {
>     echo urlencode($value);
>   }
> ?>
>
> It just struck me, this whole thing is about type coercion when not using
> `strict_types=1`; and the 8.1 change has introduced an inconstancy by
> deprecating the coercion of NULL, a frequently used value.

That "inconsistency" had been introduced with PHP 7.0.0, i.e. right when
scalar type declarations have been introduced.  Passing a null to a
non-nullable parameter of a *userland* function throws a TypeError:
<https://3v4l.org/lbF4h>.  As of PHP 8.1.0, internal functions behave
the same as userland function in this regard.


--
Christoph M. Becker

---

Next page: https://externals.io/message/116752.md?page=2
