How can PHP code be modified to write strings on separate lines in a file?
To write strings on separate lines in a file using PHP, you can append a newline character "\n" at the end of each string before writing it to the file. This will ensure that each string is written on a new line in the file.
<?php
// Strings to write to the file
$string1 = "Hello";
$string2 = "World";
// Open the file in append mode
$file = fopen("output.txt", "a");
// Write strings on separate lines
fwrite($file, $string1 . "\n");
fwrite($file, $string2 . "\n");
// Close the file
fclose($file);
?>
Keywords
Related Questions
- What are the best practices for optimizing image processing performance when using ImageMagick in PHP?
- How can mixing different languages in PHP code affect readability and maintenance?
- How can proper validation and escaping of user input, such as IP addresses, prevent potential security risks in PHP scripts?