flush
flush(): void
Pushes the system write buffers of PHP and its backend (CGI/web server) toward the client; does not touch ob_start user-level buffers.
C C23
#include <stdio.h>
int main(void) {
/* stdout to a pipe is block-buffered; fflush pushes buffered data onward */
fputs("chunk-1\n", stdout);
fflush(stdout);
fputs("chunk-2\n", stdout);
return 0;
}
chunk-1 chunk-2
C++ C++26
#include <iostream>
int main() {
// std::endl = newline + flush; std::flush pushes without adding a newline
std::cout << "chunk-1\n" << std::flush;
std::cout << "chunk-2\n";
return 0;
}
chunk-1 chunk-2
C# v11.0.100
// Console.Out has AutoFlush=true by default; Flush() is the explicit push
// point for wrapped, buffered writers
Console.WriteLine("chunk-1");
Console.Out.Flush();
Console.WriteLine("chunk-2");
chunk-1 chunk-2
Go v1.27.4
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
w := bufio.NewWriter(os.Stdout)
fmt.Fprintln(w, "chunk-1")
w.Flush() // explicit push - the Go counterpart of flush()
fmt.Fprintln(w, "chunk-2")
w.Flush()
}
chunk-1 chunk-2
Java v27.0.1
// System.out is a PrintStream over a buffered stream; flush pushes it out
class Main {
public static void main(String[] args) {
System.out.println("chunk-1");
System.out.flush();
System.out.println("chunk-2");
}
}
chunk-1 chunk-2
JavaScript v26.13.1
// JS has no flush primitive: console/process.stdout is a stream that
// writes through directly (Node stdout is line-buffered on TTY, may buffer
// to pipes, but there is no user-callable flush)
console.log("chunk-1");
process.stdout.write("chunk-2\n");
chunk-1 chunk-2
PHP v8.6.0
// flush pushes the SYSTEM-level write buffers (PHP + backend like CGI or
// the web server) toward the client; user-level buffers started by ob_start
// are NOT touched - two different layers
// In CLI there is only the system layer, so the call is harmless yet honest:
echo "chunk-1\n";
flush();
echo "chunk-2\n";
chunk-1 chunk-2
Python v3.15.2
import sys
# print(flush=True) pushes stdout immediately; sys.stdout.flush() is the
# manual push matching PHP flush()
print("chunk-1", flush=True)
sys.stdout.flush()
print("chunk-2")
chunk-1 chunk-2
Rust v1.99.1
use std::io::{self, Write};
fn main() {
let stdout = io::stdout();
let mut lock = stdout.lock();
writeln!(lock, "chunk-1").unwrap();
lock.flush().unwrap(); // explicit push - the Rust counterpart of flush()
writeln!(lock, "chunk-2").unwrap();
}
chunk-1 chunk-2
TypeScript v26.13.1
// TS: same runtime as JS — no flush primitive; process.stdout is a
// Writable stream, writes go through directly
const out: NodeJS.WriteStream = process.stdout;
out.write("chunk-1\n");
out.write("chunk-2\n");
chunk-1 chunk-2
Language differences
C: fflush(stdout) only pushes the C runtime's own buffer — the kernel pipe buffer cannot be pushed from user code; which layer buffers depends on the layer (language-library buffer vs kernel buffer).
C++: std::endl is newline + flush shorthand; "\n" does not flush — prefer "\n" for high-frequency output.
C#: Console.Out has AutoFlush on by default, so daily code needs no manual Flush; only buffered custom StreamWriters need an explicit close-out.
Go: buffering is batched by bufio.Writer and closed out with Flush (example); writing to os.Stdout directly is immediately visible, no separate flush concept.
Java: System.out.println auto-flushes, print does not and needs a manual flush() (example).
JavaScript: console.log has no flush concept — Node's stdout is a stream, line-buffered on TTY, OS-buffered to pipes, with no user-callable flush primitive.
PHP: two-layer buffering model — ob_start opens the user-level buffer while flush() pushes the system-level one (PHP itself plus the CGI/web-server backend); to send everything call ob_flush() + flush() as a pair, in that order; in CLI only the system layer exists, so flush has no visible effect but is harmless.
Python: sys.stdout.flush() pushes the language-library layer; kernel/pipe layers cannot be pushed from user code — the same two-layer model as PHP/C; print(flush=True) is the common shortcut.
Rust: std::io::Write::flush is a trait method; stdout is line-buffered (LineWriter) so whole-line writes push through; manual flush is only needed for fragments without a newline.
TypeScript: same as JavaScript — stdout is a pass-through Writable stream, no flush primitive.