file_put_contents() With Array Argument

By Gina Peter Banyard

<?php

file_put_contents('/path/to/file.txt', [1,2,3]);

print file_get_contents('/path/to/file.txt');
// print 123

The second argument of file_put_contents() is typed as mixed. It may be a string, which is the most common case. It may be a stream, (aka, a resource), which pours a file into a file.

It also may be an array. PHP simply writes the elements of the array in the file, like an implode('', $array).

See Also

PHP Features

Last updated: 14 July 2026