The string merging function in php is "implode()". The implode() function can convert a one-dimensional array into a string, merge the array elements into a string and return it. The syntax is "implode($glue, $array)"; the parameter "$glue" can be omitted, specifying the number of array elements. The content placed between them, that is, the connector, is used to connect each element of the array together. By default, the parameter "$glue" is an empty string.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php string merging function implode( ); php string splitting function explode().
implode can combine and concatenate the elements in the array into a string and return it.
PHP implode() function
implode() function returns a string composed of array elements.
implode($glue, $array)
$glue is used to set a string, indicating that $glue is used to connect each element of the array together. By default, $glue is an empty string.
$array is the array that needs to be converted.
Tip: The $glue parameter of the implode() function is optional and can be omitted.
Example: Use the implode() function to combine array elements into a string
<?php header('content-type:text/html;charset=utf-8'); $arr = [1,2,3,4,5,6,7,8,9]; $str = implode($arr); echo $str.'<br>'; $str = implode(' ',$arr); echo $str.'<br>'; $str = implode(',',$arr); echo $str.'<br>'; $str = implode('--',$arr); echo $str.'<br>'; ?>
Extended knowledge:
explode() is the php string delimiter function.
explode() function will split the string into a substring according to the delimiter, then combine these substrings into an array and return it.
explode($delimiter, $string [, $limit])
Example:
<?php header('content-type:text/html;charset=utf-8'); $str = "1,2,3,4,5,6,7,8,9"; $arr = explode(',', $str); var_dump($arr); $arr = explode(',', $str, 3); var_dump($arr); $arr = explode(',', $str, -2); var_dump($arr); $arr = explode(',', $str, 0); var_dump($arr); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the string merging function in php. For more information, please follow other related articles on the PHP Chinese website!