What is the difference between implode() & join()

Php

Php Problem Overview


What is the difference between implode() & join() as both work the same way.

<?php
    $array = array(1,2,3);
    
    echo join(",", $array); // output 1,2,3
    echo implode(",", $array); // output 1,2,3
?>

Is there is any advantage of using one over another?

Php Solutions


Solution 1 - Php

They are aliases of each other. They should theoretically work exactly the same. Although, using explode/implode has been shown to increase the awesomeness of your code by 10%

Solution 2 - Php

Join: Join is an Alias of implode().

Example:

<?php
$arr = array('Test1', 'Test2', 'Test3');
$str = join(",", $arr);
echo $str; 
?>

> Output: Test1,Test2,Test3.

Implode: implode Returns a string from array elements.

Example:

<?php
$arr = array('Test1', 'Test2', 'Test3');
$str = implode(",", $arr);
echo $str; 
?>

> Output: Test1,Test2,Test3.

UPDATE:

I tested them in Benchmark and they are same in speed. There is no difference between them.

Solution 3 - Php

join() is an alias for implode(), so implode is theoretically more "PHP native" though there is absolutely no performance increase to be gained by using it.

On the other hand, join() is found in, amongst other languages, Perl, Python and ECMAScript (including JavaScript) and so is much more portable in terms of comprehensibility to a wider audience of programmers.

So although explode and implode are unarguably way more dramatic-sounding, I would vote for join as a more universal way to express the concatenation of every value of an array into a string.

Solution 4 - Php

Can't think of any. The one is an alias of the other. They both combine array elements into a string.

Solution 5 - Php

In short the reality is that in php, join() and implode() methods function the same way.

One can be interchanged for the other as they work in the same way.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKhawer ZeshanView Question on Stackoverflow
Solution 1 - PhpPlausibleSargeView Answer on Stackoverflow
Solution 2 - PhpErman BeleguView Answer on Stackoverflow
Solution 3 - PhpTom AugerView Answer on Stackoverflow
Solution 4 - PhpveeView Answer on Stackoverflow
Solution 5 - PhpAsiamah AmosView Answer on Stackoverflow