How to Split a Comma Delimited String to an Array with PHP

Almost every developer has been in a situation when it is necessary to split a comma delimited string into an array while working with PHP. This snippet is dedicated to discovering a solution to that issue. In practice, it can be easily accomplished with the help of explode.

Here is an example:

<?php

$myString = "9,[email protected],8";
$myArray = explode(',', $myString);
print_r($myArray);

?>

Watch a course Learn object oriented PHP

The output will look like this:

Array ( [0] => 9, [1] => '[email protected]', [2] => 8 )

Describing PHP explode

PHP explode is supported on PHP 4, PHP 5, and PHP 7 versions. It is aimed at splitting the string by a string. It can return an array of strings. Each of them is considered a substring of a string made up from splitting that on the borders shaped by the string delimiter.