Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why implode() can accept its parameters in either order, explode() cannot.?

Tags:

php

Although implode() can, for historical reasons, accept its parameters in either order, explode() cannot. Why?

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

is similar to

$array = array('lastname', 'email', 'phone');
$comma_separated = implode( $array,",");

Why?

like image 641
shubhendu Avatar asked Oct 29 '25 05:10

shubhendu


1 Answers

As per implode() and explode() documentation, they only said it's historical reason.

This is what found in quora. Also the reason sounds promising!!

Part of the reason, I guess, is that both parameters of explode are strings, thus it would be difficult to tell which is the delimiter and which is the original string if they're swapped. Implode, however, takes a string (glue) and an array (pieces) as its parameters. It's at least easy to tell them apart.

The 'historical reason' might refer to an API design guideline change, where it was decided that 'smaller' parameters (needle, glue) should be before 'bigger' parameters (haystack, pieces). Implode might have used the reverse order before that.

like image 154
Parixit Avatar answered Oct 30 '25 23:10

Parixit