Hướng dẫn
Quảng cáo

Hàm join() trong PHP

Hướng dẫn cách sử dụng hàm join() trong lập trình PHP

Tác dụng của hàm join()

The join() function join array elements into a single string.

This function is an alias of implode() function.

The following table summarizes the technical details of this function.

Return Value: Returns a string created by joining array's values.
Changelog: Passing the separator after the array has been deprecated since PHP 7.4.0
Version: PHP 4+

Syntax

The basic syntax of the join() function is given with:

join(separator, array);

The following example shows the join() function in action.

Example

<?php
// Sample array
$array = array("one", "two", "three", "four", "five");

// Creating comma separated string from array elements
echo join(",", $array);
?>

Parameters

The join() function accepts the following parameters.

Parameter Description
separator Optional. Specifies the string to be inserted between each element of the array while joining. It is also called glue string. Default is an empty string ("").
array Required. Specifies the array to be joined.

More Examples

Here're some more examples showing how join() function actually works:

The following example shows how to join array elements using different characters.

Example

<?php
// Sample array
$array = array("one", "two", "three", "four", "five");

// Imploding array with different separators
echo join(", ", $array); // comma plus space
echo join("_", $array);  // underscore
echo join("-", $array);  // hyphen
echo join(" ", $array);  // space
?>

Bài viết này đã giúp ích cho bạn?

Bài viết mới

Advertisements