You are here: Home / Topics / How to convert a string into Array elements in PHP

How to convert a string into Array elements in PHP

Filed under: PHP on 2022-11-17 06:48:22

The below program is to split a string as array elements based on delimiter using PHP explode() function. The PHP echo statement is used to output the result on the screen. An array is a single variable which can store multiple values at a time. The PHP explode() function is used to break a string into an array.

explode() function in php


Example

<!DOCTYPE html>
<html>
<body>

<?php
$str = "Have Courage. Live Free";
print_r (explode(" ",$str));
?>

</body>
</html>


Output

Array ( [0] => Have [1] => Courage. [2] => Live [3] => Free )

About Author:
D
Deepak Sahoo     View Profile
I am preparing for UPSC. My dream is to be IAS.