ob_implicit_flush
ob_implicit_flush(bool $enable = true): void
切换隐式刷出模式,开启后每次输出调用自动 flush 推送。
C C23
#include <stdio.h>
int main(void) {
// C 无隐式刷出开关:stdout 默认行缓冲(终端)或全缓冲(管道),fflush 手动推送
printf("stream: line 1\n");
// 手动刷出等价物
fflush(stdout);
printf("stream: line 2\n");
printf("done\n");
return 0;
}
stream: line 1 stream: line 2 done
C++ C++26
#include <iostream>
int main() {
// std::endl = 换行 + flush(写法层面的隐式刷出);"\n" 不刷出
std::cout << "stream: line 1" << std::endl;
std::cout << "stream: line 2\n";
// 手动刷出等价物
std::cout.flush();
std::cout << "done\n";
return 0;
}
stream: line 1 stream: line 2 done
C# v11.0.100
// Console.Out 默认 AutoFlush=true,等效 PHP 的隐式刷出已常开
Console.WriteLine("stream: line 1");
Console.WriteLine("stream: line 2");
Console.WriteLine("done");
stream: line 1 stream: line 2 done
Go v1.27.4
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
// Go 无隐式刷出开关:bufio.Writer 攒批,Flush 才落标准输出
w := bufio.NewWriter(os.Stdout)
fmt.Fprintln(w, "stream: line 1")
fmt.Fprintln(w, "stream: line 2")
w.Flush()
fmt.Println("done")
}
stream: line 1 stream: line 2 done
Java v27.0.1
public class Main {
public static void main(String[] args) {
// System.out 的 println 自带 autoFlush;print 不刷,需手动 flush
System.out.println("stream: line 1");
System.out.print("stream: line 2\n");
// 手动刷出等价物
System.out.flush();
System.out.println("done");
}
}
stream: line 1 stream: line 2 done
JavaScript v26.13.1
// console.log 无 flush API:Node stdout 写入即出,无 PHP 式攒批语义
console.log("stream: line 1");
console.log("stream: line 2");
console.log("done");
stream: line 1 stream: line 2 done
PHP v8.6.0
// 开启隐式刷出:每次输出调用后自动 flush 推送(无需手动 ob_flush)
ob_implicit_flush(true);
echo "stream: line 1\n";
echo "stream: line 2\n";
// 关闭隐式刷出:回到攒批或手动控制模式
ob_implicit_flush(false);
echo "done\n";
stream: line 1 stream: line 2 done
Python v3.15.2
import sys
# Python 无全局隐式刷出开关:块缓冲时手动 flush 或 print(flush=True)
sys.stdout.write("stream: line 1\n")
# 手动刷出等价物
sys.stdout.flush()
print("stream: line 2", flush=True)
print("done")
stream: line 1 stream: line 2 done
Rust v1.99.1
use std::io::{self, Write};
fn main() {
// stdout 是行缓冲(LineWriter):整行写出即达隐式刷出效果;无换行的片段要手动 flush
print!("stream: line 1\n");
// 手动刷出等价物
io::stdout().flush().unwrap();
println!("stream: line 2");
println!("done");
}
stream: line 1 stream: line 2 done
TypeScript v26.13.1
// console.log 无 flush API:Node stdout 写入即出,无 PHP 式攒批语义
console.log("stream: line 1");
console.log("stream: line 2");
console.log("done");
stream: line 1 stream: line 2 done
语言差异说明
C:无全局开关——stdout 遇管道默认全缓冲;setvbuf(stdout, NULL, _IONBF, 0) 可关缓冲(等效永远隐式刷出),fflush 手动推。
C++:惯用 std::endl(换行+flush)实现行级推送;std::unitbuf 设为每次输出后自动 flush,接近 PHP 隐式刷出。
C#:Console.Out 默认 AutoFlush=true,相当于隐式刷出常开;要攒批得自己包 StreamWriter。
Go:没有隐式刷出开关——直接写 os.Stdout 即时可见;bufio.Writer 攒批后靠 Flush 收口。
Java:System.out 的 println 自带 autoFlush,print 不刷需手动 flush();重建 PrintStream 时可自定义 autoFlush。
JavaScript:console.log 没有 flush 概念,Node 的 stdout 写入即出(TTY 行缓冲,管道下仍有 OS 级缓冲)。
PHP:ob_implicit_flush(true) 开启后每次输出调用自动 flush,实时推送日志场景利器;注意它只改变何时送出、不改最终内容——CLI 里一次捕获看不到差异,实时流才能看到。
Python:无全局开关;块缓冲场景用 flush=True 或 sys.stdout.flush(),-u/PYTHONUNBUFFERED=1 可全局无缓冲(等效隐式刷出)。
Rust:std::io::Stdout 是 LineWriter——整行写即推出,等效行级隐式刷出;print! 无换行时必须手动 flush()。
TypeScript:同 JavaScript,console.log 无 flush 概念。