Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
<?php $path = '/var/data/log.txt'; // Write (overwrites existing content) file_put_contents($path, "Hello, World! "); // Append file_put_contents($path, "Another line ", FILE_APPEND | LOCK_EX); // Write array of lines $lines = ['Line 1', 'Line 2', 'Line 3']; file_put_contents($path, implode(" ", $lines) . " "); // Low-level write with fopen $handle = fopen($path, 'a'); // 'a' = append, 'w' = overwrite if ($handle === false) { throw new RuntimeException("Cannot open $path for writing"); } try { fwrite($handle, date('Y-m-d H:i:s') . " — Event logged "); fwrite($handle, "Additional data "); } finally { fclose($handle); } // Atomic write — write to temp then rename (prevents partial reads) $tmpPath = $path . '.tmp.' . uniqid('', true); file_put_contents($tmpPath, $newContent, LOCK_EX); rename($tmpPath, $path); // atomic on most systems
Result
Open