Table of contents

Print & Write Array Values to Files in PHP

In this post, I will share a short and simple code on how to print and write array values to files in PHP. If you have a task or problem that needs to write the array value to a file then this is for you. In this example, I'm using file_put_contents() to put the value to a file. And PHP_EOL to newline after loop the array values.

 

<?php

$contents = '';

$programmingLanguages = ['PHP', 'PYTHON', 'C#', 'C++'];

foreach ($programmingLanguages as $programmingLanguage) {
    $contents .= $programmingLanguage.PHP_EOL;
}

file_put_contents('filename.txt', $contents, true);

?>

 

Thanks for reading. I hope it helps. Cheers :)