You are currently viewing How to Use Explode Function in PHP with Example Codes
  • Post category:PHP

How to Use Explode Function in PHP with Example Codes

Explode Function in PHP

In this article, we will discuss PHP explode() function. It is used to splits a string into array elements. We must pass two arguments for this function, one is character and another one is array name. Let’s see the example code:

<?php
$str = 'red, green, blue, orange'; // define string
//convert string to array
$arr = explode(',' , $str);
print_r($arr);
?>

Output:
Array ( [0] => red[1] => green [2] =>blue [3]=> orange)

  1. In the above code, we have created a string “$str” and assigned some colors as values separated with a comma (,).
  2. If we want to convert that into an array, we need to use explode() function and pass a comma and $str.
  3. Then we print the variable “$arr”. See the above output, a string is converted to an array successfully.

Leave a Reply