Using PHPs implode() and explode() Built-in Functions

In this article, you will learn how to use the PHPs implode() and explode() built-in functions. The tutorial aims to provide the IT/CS students and new programmers a reference for widening their knowledge and enhancing their PHP Programming capabilities. Here, snippets that demonstrate the usage of the said PHP functions are provided.

PHP comes with built-in methods and functions that are helpful and useful for developers to achieve their desired features for their projects or web applications. The implode() and explode() functions are one of these and are available from PHP version 4 up to the latest which is PHP8. These functions are used for manipulating arrays and strings.

What is implode() in PHP?

The implode() function in PHP is a built-in function that allows you to combine the array of data into one string. Developers can simply join the array items and set a string between each item.

How to use PHP implode() function?

 <?php implode(str $seperator, array $array) ?>

Parameters

  • (str)$seperator

    • The string that is placed between the array items when combined.
  • (arr)$array

    • The array of strings to combine or implode.

Example

Here is an example snippet that demonstrates the usage of implode() function of PHP.

  1. <?php
  2. //Sample Arrays
  3. $array1 = array("Hello", "Wold!!!");
  4. $array2 = array("I", "Love", "SourceCodester!");
  5. $array3 = array("PHP", "JavaScript", "Python");
  6.  
  7. //Combining Arrays
  8. $arrtostring1 = implode(" ", $array1);
  9. $arrtostring2 = implode(" ", $array2);
  10. $arrtostring3 = implode(", ", $array3);
  11.  
  12. echo $array1."<br>";
  13. echo $array2."<br>";
  14. echo $array3;
  15. ?>

Result

PHP Implode Snippet - Result

What is explode() in PHP?

The explode() function in PHP is a built-in function that allows you to split the string into pieces. The developers can explode the string using the given separator. The explode function result can be limited depending on the developer's given count of items to return.

How to use PHP explode() function?

  1. <?php explode(str $seperator, str $string, int $limit delault:PHP_INT_MAX) ?>

Parameters

  • (str)$seperator

    • The string checks and serves as the boundaries of the items for splitting.
  • (str)$string

    • The string to split using the method/function.
  • (int)$limitDefault: PHP_INT_MAX

    • The limit count of the result's return array items and if the entered value is positive.
    • If negative, the function will return all the items except the last (n) item/s.

Example

Here is an example snippet that demonstrates the usage of explode() function of PHP.

  1. <?php
  2. $string1 = "Hello Wold!!!";
  3. $string2 = "I Love SourceCodester!";
  4. $string3 = "PHP, JavaScript, Python";
  5. $splitString1 = explode(" ", $string1);
  6. $splitString2 = explode(" ", $string2);
  7. $splitString3 = explode(", ", $string3, -1);
  8.  
  9. print_r($splitString1);
  10. echo "<br>";
  11. print_r($splitString2);
  12. echo "<br>";
  13. print_r($splitString3);
  14. ?>

Result

PHP Implode Snippet - Result

There you go! I hope this Usage of PHPs implode() and explode() built-in function tutorial helps you and enhances your programming knowledge and capabilities.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding :)

Add new comment