Using PHP's file() Function to Read Files into an Array

This article explains how the PHP file() function reads a file's contents into an array, details its syntax and parameters, demonstrates basic and flag‑based usage with code examples, and highlights important behavior such as line handling and newline removal.

php Courses
php Courses
php Courses
Using PHP's file() Function to Read Files into an Array

In PHP development, reading the contents of a file for processing is a common task. PHP provides a range of file handling functions, and one of the most frequently used is the file() function.

The file() function reads the contents of a file into an array, making it convenient to manipulate and operate on the file data.

The syntax of the file() function is:

array file ( string $filename [, int $flags = 0 [, resource $context ]] )

Where $filename is the name of the file to read (absolute or relative path), $flags is an optional parameter to specify how the file should be read, and $context is an optional resource that defines the file's context.

Below is a concrete example that demonstrates how to use file() to read a file's contents into an array:

<?php
// Read file contents into an array
$fileContent = file('data.txt');

// Output array contents
foreach ($fileContent as $line) {
    echo $line . "<br>";
}
?>

In this example, the file() function reads the file data.txt into the array $fileContent. The foreach loop then iterates over each line of the array and outputs it to the browser, allowing the entire file to be displayed line by line.

When reading a file, file() automatically treats each line as a separate array element and, by default, removes the trailing newline character from each line.

Beyond simply reading file contents into an array, the file() function can accept additional parameters to modify its behavior. For instance, the second parameter $flags can be set to FILE_IGNORE_NEW_LINES to ignore the newline character at the end of each line.

Example using the FILE_IGNORE_NEW_LINES flag:

<?php
// Read file contents into an array and ignore newline characters
$fileContent = file('data.txt', FILE_IGNORE_NEW_LINES);

// Output array contents
foreach ($fileContent as $line) {
    echo $line . "<br>";
}
?>

This example reads data.txt into $fileContent while ignoring the newline characters, demonstrating how the flag alters the function's default behavior.

In summary, the file() function is a highly practical file handling utility in PHP that efficiently reads a file's contents into an array, simplifying further processing and improving development efficiency.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPArrayCode Examplefile-handling
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.