Re: BC break in master (7.2) - Distinguish betweenunmatchedsubpatterns and empty matches in preg_*()

From: Date: Tue, 16 May 2017 22:21:31 +0000
Subject: Re: BC break in master (7.2) - Distinguish betweenunmatchedsubpatterns and empty matches in preg_*()
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-99064@lists.php.net to get a copy of this message
On 17.05.2017 at 00:13, Sara Golemon wrote: > On Tue, May 16, 2017 at 5:03 PM, Christoph M. Becker <cmbecker69@gmx.de> wrote: > >>>> I suggest not to set unmatched subpatterns at all, >>> >>> Can you give an example of expected output ? >>> (I think stable position for matched pattern is important) >>> >>> php -r 'var_dump(preg_match("/(a)?([a-z]*)(\d*)/", "123", >>> $matches), >>> $matches);' >> >> See the pcretest results danielklein posted in his last comment on >> <https://bugs.php.net/61780>. Basically, instead of >> assigning NULL as >> values, the array indexes could be skipped, so that array_key_exists() >> can be used to determine whether a certain subpattern matched or not. > > Well, but isset() would do this already with NULLs. > > $x = ['foo'=>NULL]; > // isset($x['foo'] === false) > > I know people are generally getting trained to use array_key_exists() > to avoid this "set but null" issue, but this is a case where that > behavior of isset() is actually quite useful. Personally, I rarely use array_key_exists(), but others may prefer it, and it appears to be cleaner not to set unmatched subpatterns at all. And if we're going to add a flag anyway, it appears to be reasonable to change the behavior right away, i.e. to introduce something like PREG_SKIP_UNMATCHED instead of PREG_UNMATCHED_AS_NULL. -- Christoph M. Becker

« previous php.internals (#99064) next »