PHP join() Function
The join() function of PHP returns a string from the elements of an array. This function is an alias of the implode() function.
Syntax
1 2 3 |
join(separator,array) |
Parameter
separator(Optional)– Specifies what to put between the array elements. The default value is empty string (“”).
array(required)- This parameter represents the array to join to a string
Return
This function returns a string from the elements of an array.
Example 1
1 2 3 4 5 6 7 8 9 10 |
<?php // specifying the array $array = array('This','is','a','PHP','Join','Function'); echo "Before the join() function...\narray('This','is','a','PHP','Join','Function')"; // returning a string from the elements of the given array. echo "\nAfter the join() function...\n"; echo join(" ",$array); ?> |
Output
1 2 3 4 5 6 |
Before the join() function... array('This','is','a','PHP','Join','Function') After the join() function... This is a PHP Join Function |
Example 2
1 2 3 4 5 6 7 8 9 10 |
<?php // specifying the array $array = array('This','is','a','PHP','Join','Function'); echo "Before the join() function...\narray('This','is','a','PHP','Join','Function')"; // returning a string from the elements of the given array. echo "\nAfter the join() function...\n"; echo join("+",$array); //putting + between the specified array elements. ?> |
Output
1 2 3 4 5 6 |
Before the join() function... array('This','is','a','PHP','Join','Function') After the join() function... This+is+a+PHP+Join+Function |
Example 3
1 2 3 4 5 6 7 |
<?php $arr = array('!', '@', '#', '$', '%', '^', '&', '*', '(', '}','~'); echo "The special characters are: \n"; echo join("\n", $arr); ?> |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
The special characters are: ! @ # $ % ^ & * ( } ~ |