|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-04-28 11:19 UTC] dan at yes dot lt
Description:
------------
explode() do not explodes string if $limit argument is null, but explodes if no $limit argument given.
Reproduce code:
---------------
print_r(explode(',', 'a,b,c'));
print_r(explode(',', 'a,b,c', null));
Expected result:
----------------
Array
(
[0] => a
[1] => b
[2] => c
)
Array
(
[0] => a
[1] => b
[2] => c
)
Actual result:
--------------
Array
(
[0] => a
[1] => b
[2] => c
)
Array
(
[0] => a,b,c
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 07 17:00:01 2025 UTC |
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 ?..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)