What function is used to read a line from a file in PHP, and what are its limitations?
To read a line from a file in PHP, you can use the `fgets()` function. However, it is important to note that `fgets()` reads the file line by line, so it may not be suitable for reading large files as it can consume a lot of memory. If you need to read a large file, consider using `fread()` instead.
// Open the file for reading
$handle = fopen("example.txt", "r");
// Read and output each line from the file
while(!feof($handle)){
$line = fgets($handle);
echo $line;
}
// Close the file
fclose($handle);
Keywords
Related Questions
- What are some common mistakes to avoid when writing complex PHP queries with subselects and clauses like NOT IN?
- What are the best practices for handling file downloads and redirects in PHP scripts to avoid conflicts?
- What are the advantages of using HTML emails over plain text emails in PHP for encoding special characters?