file_get_contents

file_get_contents(string $filename): string|false

Reads an entire file into a string.

C C23

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

int main(void) {
    const char *text = "hello c";
    FILE *out = fopen("demo.txt", "w");
    fwrite(text, 1, strlen(text), out);
    fclose(out);
    FILE *in = fopen("demo.txt", "r");
    char buf[64];
    size_t n = fread(buf, 1, sizeof buf - 1, in);
    buf[n] = '\0';
    fclose(in);
    printf("%s\n", buf);
    return 0;
}
Output
hello c

C++ C++26

C++26
#include <fstream>
#include <iostream>
#include <iterator>
int main() {
    {
        std::ofstream out("demo.txt");
        out << "hello cpp";
    }
    std::ifstream in("demo.txt");
    std::string content((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
    std::cout << content << "\n";
    return 0;
}
Output
hello cpp

C# v11.0.100

v11.0.100
File.WriteAllText("demo.txt", "hello csharp");
Console.Write(File.ReadAllText("demo.txt"));
Output
hello csharp

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "os"
)

func main() {
    os.WriteFile("demo.txt", []byte("hello go"), 0644)
    data, _ := os.ReadFile("demo.txt")
    fmt.Print(string(data))
}
Output
hello go

Java v27.0.1

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

public class Main {
    public static void main(String[] args) throws Exception {
        Path path = Path.of("demo.txt");
        Files.writeString(path, "hello java");
        System.out.print(Files.readString(path));
    }
}
Output
hello java

JavaScript v26.13.1

v26.13.1
const fs = require("node:fs");
fs.writeFileSync("demo.txt", "hello node");
console.log(fs.readFileSync("demo.txt", "utf8"));
Output
hello node

PHP v8.6.0

v8.6.0
file_put_contents("demo.txt", "hello php");
echo file_get_contents("demo.txt");
Output
hello php

Python v3.15.2

v3.15.2
from pathlib import Path

Path("demo.txt").write_text("hello python")
print(Path("demo.txt").read_text())
Output
hello python

Rust v1.99.1

v1.99.1
use std::fs;

fn main() {
    fs::write("demo.txt", "hello rust").unwrap();
    print!("{}", fs::read_to_string("demo.txt").unwrap());
}
Output
hello rust

TypeScript v26.13.1

v26.13.1
import * as fs from "node:fs";
fs.writeFileSync("demo.txt", "hello ts");
console.log(fs.readFileSync("demo.txt", "utf8"));
Output
hello ts

Language differences

C: has no one-shot read — fopen+fread into a buffer (the example writes the file first for the demo).

C++: has no one-shot read — reads all characters with std::ifstream plus istreambuf_iterator.

C#: File.ReadAllText is the equivalent one-shot text read.

Go: uses os.ReadFile returning a byte slice.

Java: Java 11+ Files.readString is the equivalent one-shot text read.

JavaScript: Node uses fs.readFileSync with an explicit encoding.

PHP: file_get_contents reads the whole file into one string and returns false on failure.

Python: Path.read_text() is the equivalent one-shot text read.

Rust: fs::read_to_string is the equivalent one-shot text read.