Using PHP 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 file() Function to Read Files into an Array

In PHP development, reading a file's contents for processing is common, and the file() function is a frequently used tool that loads a file into an array for easy manipulation.

The syntax of file() is:

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

The $filename argument specifies the file path (absolute or relative). The optional $flags argument controls how the file is read, and $context can provide a stream context.

Below is a basic example that reads a file into an array and outputs each line:

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

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

In this example, file() reads data.txt into $fileContent, and a foreach loop prints each line to the browser, demonstrating how the function returns each line as a separate array element.

When reading files, file() automatically splits the file by lines and removes the trailing newline character from each element.

Beyond basic reading, file() accepts flags to modify its behavior. For instance, using the FILE_IGNORE_NEW_LINES flag prevents the newline character from being stripped.

Example with the FILE_IGNORE_NEW_LINES flag:

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

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

This code reads data.txt into $fileContent while preserving each line exactly as it appears, thanks to the FILE_IGNORE_NEW_LINES flag.

In summary, the file() function is a practical file‑handling utility that efficiently loads a file into an array, simplifying further processing and improving development productivity.

PHP实战开发极速入门

扫描二维码免费领取学习资料

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.

BackendPHPArrayfile-handlingphp-functions
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.