Implode and Explode Function in PHP

Implode and Explode function of PHP is very useful to convert the array into string and string into an array for example when we collect data by using checkbox input then we receive data in an array format then we have two choices to get checkbox value either by using a loop or we can convert the array into a string by using implode function.


Implode() function

Implode() function is used to convert the array into a string, it uses any symbol to join an array. It is commonly used to convert checkbox values into strings to store easily in a database.


Syntax:

implode(‘symbol’,’array’);


Example:

<?php
 
if(isset($_POST['submit']))
{
                 $price=implode("#",$_POST['price']);
                 
                 echo $price;
}
 
?>
 
 
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Implode</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
 
<div class="container">
  <form method="post">
    <div class="form-group">
      <label for="email">Price:</label>
      <input type="checkbox" class="form-control" name="price[]">1000
 <input type="checkbox" class="form-control" name="price[]">2000
 <input type="checkbox" class="form-control" name="price[]">3000
    </div>       
<button type="submit" name="submit" class="btn btn-primary">Submit</button>  
</form>
</div>  
</body>
</html>


In this example, implode is used to convert checkbox value into a string with the # symbol.


Explode() function

Explode() function is used to convert a string into an array or we can say that show implode() function value individually then we use explode.


Syntax:

explode(“symbol”,’ array’);


Example:

<?php
$number = '1,2,3,4,5';
$arr = explode(',',$number);
foreach($arr as $i)
echo($i.'<br>');
?>


Here explode function convert string into an array and we have used for each to show all elements of an array one by one.

Categories: web development php

Trending Courses

CodeIgniter

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Advance Digital Marketing

Regular : 6 Months

Fastrack : 3 Months

Crash : 2 Months

React JS

Regular : 45 Days

Fastrack : 20 Days

Crash : 7 Days

Laravel

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Front End Developer

Regular : 7 Month

Fastrack : 4 Month

Crash : 2 Month

Related Blogs

Request For Demo