PHP implode () function differs from the use of the explode () function

Source: Internet
Author: User
The explode () function in PHP is to split another string with one string and return an array of strings. The implode () function returns a string that is composed of array elements. The two functions are between the strings and the array

function of mutual conversion.

First, from the usage point of view, one is to concatenate the array elements into a string, one to divide the string into the array. So it's different from the usage.

Then there is the difference in the receiving parameters, explode () because of historical reasons can not receive two parameter order, must ensure that the separator parameter before the string parameter;

Implode () can accept two parameter sequences.

Example

Implode () Join function:

<?php$array = Array (' a ' = = 1, ' B ' =>2, ' C ' =>3, ' d ' =>4); $string = Implode ("-", $array);   echo $string;? >

With running results such as:

Explode () Split function:

Note that this delimiter is present in the string, and we still use the above results as an example.

<?php$string = "1-2-3-4"; $array = Explode ("-", $string); echo "<pre>";p Rint_r ($array);? >

Code Run Result:

The implode () function and the scene used by the explode () function:

The explode () method is often used to enter multiple options, separated by a specific delimiter, to convert an array.

Implode () converts an array to a string, when constructing an SQL statement, for example

Insert into table (COL1,COL2,COL3) VALUES (' value1 ', ' value2 ', ' value3 ')

This makes it easier to construct.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.