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 differences between using pathinfo() and getimagesize() functions in PHP for file manipulation tasks?
- How can PHP developers ensure that selected values in dropdowns are correctly pre-selected based on database values?
- How can PHP developers effectively handle BBcode tags like LINKS, QUOTE, CODE, LIST, SIZE, COLOR, and IMG in their applications?