file

file(string $filename, int $flags = 0, ?resource $context = null): array|false

Reads an entire file into a line array (newlines kept by default, stripped with FILE_IGNORE_NEW_LINES) - the array counterpart of file_get_contents' single string: pick file() for line-by-line work and file_get_contents for whole-text handling.

C C23

C23
#include <stdio.h>
#include <string.h>

int main(void) {
    // C has no read-all-lines function: fopen + fgets loop is the hand-rolled file()
    FILE *w = fopen("demo.txt", "w");
    fputs("alpha\nbeta\ngamma", w);
    fclose(w);

    FILE *h = fopen("demo.txt", "r");
    char lines[4][16];
    int count = 0;
    while (count < 4 && fgets(lines[count], sizeof lines[count], h)) {
        lines[count][strcspn(lines[count], "\n")] = '\0'; // strip "\n" like FILE_IGNORE_NEW_LINES
        count++;
    }
    fclose(h);
    printf("count: %d\n", count);
    printf("first: %s\n", lines[0]);
    printf("last: %s\n", lines[count - 1]);
    return 0;
}
Output
count: 3
first: alpha
last: gamma

C++ C++26

C++26
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

int main() {
    // The file() shape: getline loop into a vector
    {
        std::ofstream w("demo.txt");
        w << "alpha\nbeta\ngamma";
    }
    std::ifstream h("demo.txt");
    std::vector<std::string> lines;
    std::string line;
    while (std::getline(h, line)) { // strips "\n" like FILE_IGNORE_NEW_LINES
        lines.push_back(line);
    }
    std::cout << "count: " << lines.size() << "\n";
    std::cout << "first: " << lines.front() << "\n";
    std::cout << "last: " << lines.back() << "\n";
}
Output
count: 3
first: alpha
last: gamma

C# v11.0.100

v11.0.100
// File.ReadAllLines is the direct equivalent (newline stripping built in)
using System.IO;

File.WriteAllText("demo.txt", "alpha\nbeta\ngamma");
string[] lines = File.ReadAllLines("demo.txt"); // strips newlines like FILE_IGNORE_NEW_LINES
Console.WriteLine("count: " + lines.Length);
Console.WriteLine("first: " + lines[0]);
Console.WriteLine("last: " + lines[lines.Length - 1]);
Output
count: 3
first: alpha
last: gamma

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"os"
	"strings"
)

func main() {
	// Go: whole file then Split - the file() shape
	os.WriteFile("demo.txt", []byte("alpha\nbeta\ngamma"), 0644)
	data, err := os.ReadFile("demo.txt")
	if err != nil {
		panic(err)
	}
	lines := strings.Split(string(data), "\n")
	fmt.Printf("count: %d\n", len(lines))
	fmt.Printf("first: %s\n", lines[0])
	fmt.Printf("last: %s\n", lines[len(lines)-1])
}
Output
count: 3
first: alpha
last: gamma

Java v27.0.1

v27.0.1
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        // Files.readAllLines is the direct equivalent (newline stripping built in)
        Path p = Path.of("demo.txt");
        Files.writeString(p, "alpha\nbeta\ngamma");
        List<String> lines = Files.readAllLines(p); // strips newlines like FILE_IGNORE_NEW_LINES
        System.out.println("count: " + lines.size());
        System.out.println("first: " + lines.get(0));
        System.out.println("last: " + lines.get(lines.size() - 1));
    }
}
Output
count: 3
first: alpha
last: gamma

JavaScript v26.13.1

v26.13.1
// Node: whole-file string then split - the file() shape
const fs = require("node:fs");
fs.writeFileSync("demo.txt", "alpha\nbeta\ngamma");
const lines = fs.readFileSync("demo.txt", "utf8").split("\n"); // no trailing "\n" here
console.log("count:", lines.length);
console.log("first:", lines[0]);
console.log("last:", lines[lines.length - 1]);
Output
count: 3
first: alpha
last: gamma

PHP v8.6.0

v8.6.0
// file() = file_get_contents() + explode into lines; FILE_IGNORE_NEW_LINES strips "\n"
file_put_contents("demo.txt", "alpha\nbeta\ngamma");
$lines = file("demo.txt", FILE_IGNORE_NEW_LINES);
echo "count: ", count($lines), "\n";
echo "first: ", $lines[0], "\n";
echo "last: ", $lines[2], "\n";
Output
count: 3
first: alpha
last: gamma

Python v3.15.2

v3.15.2
# read_text() + splitlines() - splitlines() strips newlines like FILE_IGNORE_NEW_LINES
from pathlib import Path

Path("demo.txt").write_text("alpha\nbeta\ngamma")
lines = Path("demo.txt").read_text().splitlines()
print(f"count: {len(lines)}")
print(f"first: {lines[0]}")
print(f"last: {lines[-1]}")
Output
count: 3
first: alpha
last: gamma

Rust v1.99.1

v1.99.1
use std::fs;

fn main() {
    // read_to_string + lines() - lines() strips "\n" like FILE_IGNORE_NEW_LINES
    fs::write("demo.txt", "alpha\nbeta\ngamma").unwrap();
    let text = fs::read_to_string("demo.txt").unwrap();
    let lines: Vec<&str> = text.lines().collect();
    println!("count: {}", lines.len());
    println!("first: {}", lines[0]);
    println!("last: {}", lines[lines.len() - 1]);
}
Output
count: 3
first: alpha
last: gamma

TypeScript v26.13.1

v26.13.1
// Node: whole-file string then split - the file() shape
import * as fs from "node:fs";
fs.writeFileSync("demo.txt", "alpha\nbeta\ngamma");
const lines: string[] = fs.readFileSync("demo.txt", "utf8").split("\n");
console.log("count:", lines.length);
console.log("first:", lines[0]);
console.log("last:", lines[lines.length - 1]);
Output
count: 3
first: alpha
last: gamma

Language differences

C: no read-all-lines function - a fgets loop into a 2-D array is the hand-rolled version (strcspn strips the newline).

C++: a getline loop into std::vector<std::string>; no one-liner exists.

C#: File.ReadAllLines is the direct equivalent.

Go: the standard library only has os.ReadFile for whole bytes; strings.Split does the line split - file() is composed.

Java: Files.readAllLines is the direct equivalent.

JavaScript: readFileSync plus split("\n") composed; note a trailing newline leaves an empty tail piece to filter.

PHP: lines keep their newlines by default; FILE_IGNORE_NEW_LINES strips them, FILE_SKIP_EMPTY_LINES skips blanks, and invalid flags throw ValueError since PHP 8.3.

Python: splitlines() is the direct equivalent and recognises multiple line-ending styles.

Rust: str::lines() is the direct equivalent (splits on "\n" and "\r\n").

TypeScript: same as JavaScript.