Bug #32865 [Com]: explode do not works with $limit = null
| From: | restart at live dot nl | Date: | Sun, 07 Mar 2021 16:05:07 +0000 |
| Subject: | Bug #32865 [Com]: explode do not works with $limit = null | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-232609@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=32865&edit=1
ID: 32865
Comment by: restart at live dot nl
Reported by: dan at yes dot lt
Summary: explode do not works with $limit = null
Status: Not a bug
Type: Bug
Package: Strings related
Operating System: WinXP
PHP Version: 5.0.4
Block user comment: N
Private report: N
New Comment:
Ah sorry about that, thanks for the quick reply, in that case it definitely makes sense and
it's as expected.
I should have checked the php.net page first, it seems the latest version of PHPStorm still has the
wrong documentation stating the $limit parameter defaults to null. I'll try to notify them
about it.
Previous Comments:
------------------------------------------------------------------------
[2021-03-04 13:21:37] nikic@php.net
This might have been incorrectly documented at some earlier point in time, but if you check the
current documentation at https://www.php.net/explode,
you'll find that the default value for $limit is PHP_INT_MAX, not null. If you want to
replicate the default behavior, that is the value that you need to pass.
Null is very much not a "use the default" value. This is only true for parameters that use
null as default (duh).
------------------------------------------------------------------------
[2021-03-04 11:55:22] restart at live dot nl
This is definitely a bug.
16 years and this issue is still there.
The problem is this has not much to do with the loosely typing of PHP.
if a parameter defaults to null and you pass on a value of null you expect it to work the same.
Explode however treats the default value of the $limit parameter, which is null, different than a
passed on value of null.
------------------------------------------------------------------------
[2011-02-10 20:46:42] chrisbloom7 at gmail dot com
while null does evaluate to 0 when compared, most other functions treat a NULL valued argument as
though the argument did not exist, i.e. NULL == DEFAULT. The behaviour of explode to treat NULL as 0
comes back to bite us when we want to create a function that duplicates and extends explode's
functionality. Consider the following:
function explode_and_trim($separator, $string, $limit = null)
{
$a = explode($separator, $string, $limit);
foreach ($a as $k => $v) {
$a[$k] = trim($v);
}
return $a;
}
In that scenario, I cannot tell explode that there is NO limit unless I add a ridiculously
unnecessary IF block to decide if it should or should not be called with the limit argument. NULL
should be NULL, and regardless of whether PHP is loose typed or not, one can quite accurately test
whether something is actually null (is_null($n)) or zero ($n === 0)
------------------------------------------------------------------------
[2005-04-28 13:08:21] derick@php.net
No, it's how the language works.
------------------------------------------------------------------------
[2005-04-28 13:06:47] dan at yes dot lt
now I have to write..
if ($lim === null) {
$parts = explode(',', $str);
} else {
$parts = explode(',', $str, $lim);
}
..instead of..
$parts = explode(',', $str, $lim);
Isn't it some sort of crap ?..
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=32865
--
Edit this bug report at https://bugs.php.net/bug.php?id=32865&edit=1