ob_start
ob_start(callable $callback = null, int $chunk_size = 0, int $flags = PHP_OUTPUT_HANDLER_STDFLAGS): bool
Turns on output buffering; subsequent output goes into a buffer instead of being sent directly.
C C23
#include <stdio.h>
#include <string.h>
int main(void) {
// Hand-managed buffering: sprintf writes into the char array instead of stdout
char buffer[64] = "";
sprintf(buffer, "captured output");
printf("buffer holds: [%s]\n", buffer);
printf("length: %d\n", (int)strlen(buffer));
return 0;
}
buffer holds: [captured output] length: 15
C++ C++26
#include <iostream>
#include <sstream>
int main() {
// An ostringstream as the memory buffer: << goes into the buffer instead of stdout
std::ostringstream buf;
buf << "captured output";
std::string buffered = buf.str();
std::cout << "buffer holds: [" << buffered << "]\n";
std::cout << "length: " << buffered.size() << "\n";
return 0;
}
buffer holds: [captured output] length: 15
C# v11.0.100
using System.IO;
// A StringWriter as the memory buffer: Write goes into the buffer instead of Console
var sw = new StringWriter();
sw.Write("captured output");
string buffered = sw.ToString();
Console.WriteLine($"buffer holds: [{buffered}]");
Console.WriteLine($"length: {buffered.Length}");
buffer holds: [captured output] length: 15
Go v1.27.4
package main
import (
"bytes"
"fmt"
)
func main() {
// bytes.Buffer is the memory buffer: Write goes into the buffer instead of stdout
var buf bytes.Buffer
fmt.Fprint(&buf, "captured output")
buffered := buf.String()
fmt.Printf("buffer holds: [%s]\n", buffered)
fmt.Printf("length: %d\n", len(buffered))
}
buffer holds: [captured output] length: 15
Java v27.0.1
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public class Main {
public static void main(String[] args) {
// ByteArrayOutputStream captures writes; System.out can be redirected temporarily to intercept output
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream original = System.out;
System.setOut(new PrintStream(baos));
System.out.print("captured output");
System.setOut(original);
String buffered = baos.toString();
System.out.printf("buffer holds: [%s]%n", buffered);
System.out.printf("length: %d%n", buffered.length());
}
}
buffer holds: [captured output] length: 15
JavaScript v26.13.1
// JS has no output-buffering API: string concatenation is itself the buffer
let buffer = "";
buffer += "captured output";
console.log(`buffer holds: [${buffer}]`);
console.log(`length: ${buffer.length}`);
buffer holds: [captured output] length: 15
PHP v8.6.0
// Output buffering on: echo's content goes into the memory buffer instead of straight out
ob_start();
echo "captured output";
// Fetch the buffer and close it (paired with ob_get_clean)
$buffered = ob_get_clean();
echo "buffer holds: [", $buffered, "]\n";
echo "length: ", strlen($buffered);
buffer holds: [captured output] length: 15
Python v3.15.2
import io
# StringIO as the memory buffer: write goes into buf instead of stdout
buf = io.StringIO()
buf.write("captured output")
buffered = buf.getvalue()
print(f"buffer holds: [{buffered}]")
print(f"length: {len(buffered)}")
buffer holds: [captured output] length: 15
Rust v1.99.1
use std::fmt::Write;
fn main() {
// String + fmt::Write IS the buffer: writes go into the string instead of stdout
let mut buffer = String::new();
write!(buffer, "captured output").unwrap();
println!("buffer holds: [{buffer}]");
println!("length: {}", buffer.len());
}
buffer holds: [captured output] length: 15
TypeScript v26.13.1
// JS has no output-buffering API: string concatenation is itself the buffer
let buffer = "";
buffer += "captured output";
console.log(`buffer holds: [${buffer}]`);
console.log(`length: ${buffer.length}`);
buffer holds: [captured output] length: 15
Language differences
C: hand-managed buffer — sprintf into a char array; you must guarantee the buffer size yourself (snprintf is safer).
C++: std::ostringstream is the standard library's write-to-string buffer.
C#: StringWriter as an in-memory buffer; Console.SetOut redirection also works.
Go: bytes.Buffer implements io.Writer and is the ecosystem's most common in-memory buffer.
Java: ByteArrayOutputStream is a byte-level buffer; System.setOut can temporarily intercept standard output (demonstrated here).
JavaScript: there is no output-buffering API — string concatenation and template literals are the buffer; in Node you can wrap process.stdout.write.
PHP: output buffering is a signature PHP capability: after ob_start() all echo/print lands in the buffer and ob_get_clean retrieves it in one go; commonly used for compute-then-send — e.g. altering headers after they were already sent, or post-processing a fully rendered page before emitting it.
Python: io.StringIO is a string-level buffer; redirecting sys.stdout intercepts print globally.
Rust: String plus std::fmt::Write (the write! macro); format! itself is buffering in one step.
TypeScript: same as JavaScript — string concatenation is the buffer.