PHP Implode() function


 

PHP Implode() function

PHP Implode function: In this tutorial you will get to know about implode() function, which is used to convert array to string, in detail. It has general format, parameters, and the value it returns of this function. Examples of implode() function exemplifies this function.

PHP Implode function: In this tutorial you will get to know about implode() function, which is used to convert array to string, in detail. It has general format, parameters, and the value it returns of this function. Examples of implode() function exemplifies this function.

PHP implode() function

In PHP, conversion of an array to string is done by implode() function. The implode() function puts the separator (In general we use: +  , or any other special symbol) between each value of the array. This function joins array elements with a string.

General format of the implode() function is:

i)    string implode ( $separator, $array)

ii)    string implode (  $array)

iii)    string implode ( $array, $separator)

One important point about implode function is that it accepts its parameter in either order, if you don't pass any separator, even any space, PHP wont generate any warning or error message.

General description of implode() function is as follows:

General Format string implode ( $separator, $array)
Parameters $separator: Any separator like any special symbol (in general) $array: The array of strings which is to be implode
Return Value Returns a string with all its elements in same order with a separator between each element

PHP implode() Function Example 1:

<?php

$array=array("Emp01","Varun","New Delhi");

echo"<br/><b>Initially the value of \$array is:</b><br/>";

print_r($array);

echo"<br/><b>After imploding the values of \$array is:</b><br/>";

$array=implode($array,",");

print_r($array);

?>

Output:

Initially the value of $array is:
Array
(
    [0] => Emp01
    [1] => Varun
    [2] => New Delhi
)

After imploding the values of $array is:
Emp01,Varun,New Delhi

Example 2:

<?php

$array=array("Emp01","Varun","New Delhi");

echo"<br/><b>Initially the value of \$array is:</b><br/>";

print_r($array);

echo"<br/><b>After imploding the values of \$array is:</b><br/>";

$array=implode($array);

print_r($array);

?>

Output:

Initially the value of $array is:
Array
(
    [0] => Emp01
    [1] => Varun
    [2] => New Delhi
)

After imploding the values of $array is:
Emp01VarunNew Delhi

Example 3:

<?php

0

$array=array("Emp01","Varun","New Delhi");

echo"<br/><b>Initially the value of \$array is:</b><br/>";

print_r($array);

1

echo"<br/><b>After imploding the values of \$array is:</b><br/>";

$array=implode(",",$array);

print_r($array);

2

?>

Output:

Initially the value of $array is:
Array
(
    [0] => Emp01
    [1] => Varun
    [2] => New Delhi
)

After imploding the values of $array is:
Emp01,Varun,New Delhi

Note: Above three examples are same with a small change in the signature of the implode function.

3

Ads