[lang-ref] ( write_file ) ( php )
<?php
public function testWriteFile(): void
{
// file_put_contents($filename, $text)
$lines = ['test', 'test', 'test'];
$text = implode("\n", $lines);
$fname = $this->tmpDir . DIRECTORY_SEPARATOR . 'a.txt';
file_put_contents($fname, $text);
$r = file_get_contents($fname);
$this->assertSame($text, $r);
}
<?php
public function testWriteFileAlternative(): void
{
// fwrite($fp, $text)
$lines = ['test', 'test', 'test'];
$text = implode("\n", $lines) . "\n";
$p = $this->tmpDir . DIRECTORY_SEPARATOR . 'a.txt';
$fp = fopen($p, 'w');
$this->assertNotFalse($fp);
foreach ($lines as $line) {
fwrite($fp, $line . "\n");
}
fclose($fp);
$r = file_get_contents($p);
$this->assertSame($text, $r);
}