Re: note 8942 added to explode

From: Date: Sat, 07 Oct 2000 09:58:47 +0000
Subject: Re: note 8942 added to explode
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-6203@lists.php.net to get a copy of this message
v@w.cl writes: > Implode and explode can behave differently. > > I can write > > $a=implode($array,"|"); > > and $a would be the expected string separated by |. > > But if I write > > $array=explode($a,"|"); > > $array would NOT be the original $array, but an empty value. > > And neither implode or explode throws a warning about the bad parameter passing. > > http://www.php.net/manual/function.explode.php This is actually the correct behaviour. implode() can accept its two arguments in any order: the array first, or the string. This is possible because it can examine each argument to discern which is which based on type. explode() can't do that, since it can accept only strings. Essentially, by writing 'explode($a, "|")', you are telling PHP to split the string "|" on each occurrence of $a within it. To us this is obviously likely to not be what you wanted, but to PHP it makes sense--it trusts that this is what you want to do, and so it tries it and returns the result: an array containing only "|". -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.notes (#6203) next »