How can While loops be utilized to write array data line by line into a file in PHP?

To write array data line by line into a file in PHP using While loops, you can iterate over the array using a While loop and write each element to the file on a new line. This can be achieved by opening a file in write mode, looping through the array using a While loop, and writing each element to the file using fwrite() function with a newline character ("\n") appended to each element.

<?php
// Sample array data
$array = array("Line 1", "Line 2", "Line 3");

// Open a file in write mode
$file = fopen("output.txt", "w");

// Loop through the array and write each element to the file on a new line
$i = 0;
while($i < count($array)) {
    fwrite($file, $array[$i] . "\n");
    $i++;
}

// Close the file
fclose($file);
?>