Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP String join() Function



The PHP String join() function is used to join an array of elements which are separated by a string. It is an alias for the implode() method.

The join() function accepts parameters in either order. But to be consistent with explode(), you should use the documented order of parameters.

The separator parameter in join() is optional. However, it is usually advised to utilize two parameters for backward compatibility.

Syntax

Below is the syntax of the PHP String join() function −

string join ( $separator, $array )

Parameters

Here are the parameters of the join() function −

  • $separator − This is an optional parameter of string type. The array values will be joined to make a string, which will be separated by the $separator option specified below. If not provided, the default is "".

  • $array − It is the array whose values will be merged to generate a string.

Return Value

The join() function returns the type of the join() method is string. It will return the combined string created from the elements of the $array.

PHP Version

First introduced in core PHP 4, the join() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

Here is the basic example of the PHP String join() function to get the joined elements of the given array.

<?php
   $input = array('tutorials','point','Simply','Easy','Learning');
      
   echo join(" ",$input);
?>

Output

Here is the outcome of the following code −

tutorials point Simply Easy Learning

Example 2

Here is another example to show the usage of join() function to join the elements of the given array.

<?php
   $input = array('Hello','this','is', 'Tutorialspoint'); 
         
   // Join without separator 
   print_r(join($input)); 
         
   print_r("\n"); 
         
   // Join with separator 
   print_r(join("-",$input)); 
?> 

Output

This will generate the below output −

HellothisisTutorialspoint
Hello-this-is-Tutorialspoint

Example 3

Now the below code uses the join() function and separates the array elements with different characters.

<?php
   $input = array('Hello','TPians!', 'Its', 'a', 'Beautiful','Day!');
   echo join(" ",$input)."\n";
   echo join("+",$input)."\n";
   echo join("-",$input)."\n";
   echo join("X",$input);
?> 

Output

This will create the below output −

Hello TPians! Its a Beautiful Day!
Hello+TPians!+Its+a+Beautiful+Day!
Hello-TPians!-Its-a-Beautiful-Day!
HelloXTPians!XItsXaXBeautifulXDay!
php_function_reference.htm
Advertisements