ob_get_length

ob_get_length(): int|false

Returns the byte length of the active output buffer, or false if buffering is off.

C C23

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

int main(void) {
    // A hand-managed char array as the buffer: strlen counts bytes (CJK chars take 3 bytes each in a UTF-8 source file)
    char buffer[64];
    sprintf(buffer, "hello world");
    int ascii_len = (int)strlen(buffer);
    sprintf(buffer, "你好");
    printf("%d %d\n", ascii_len, (int)strlen(buffer));
    return 0;
}
Output
11 6

C++ C++26

C++26
#include <iostream>
#include <sstream>

int main() {
    // An ostringstream as the buffer: str().size() reports the byte count
    std::ostringstream buffer;
    buffer << "hello world";
    int ascii_len = static_cast<int>(buffer.str().size());
    buffer.str("");
    buffer << "你好";
    std::cout << ascii_len << " " << buffer.str().size() << "\n";
    return 0;
}
Output
11 6

C# v11.0.100

v11.0.100
using System.IO;
using System.Text;

// StringWriter counts characters; convert to PHP-style byte counts with Encoding.UTF8.GetByteCount
var buffer = new StringWriter();
buffer.Write("hello world");
int asciiLen = buffer.ToString().Length;
buffer.GetStringBuilder().Clear();
buffer.Write("你好");
Console.WriteLine($"{asciiLen} {buffer.ToString().Length} {Encoding.UTF8.GetByteCount(buffer.ToString())}");
Output
11 2 6

Go v1.27.4

v1.27.4
package main

import (
	"bytes"
	"fmt"
)

func main() {
	// bytes.Buffer.Len() reports bytes (the same basis as PHP's ob_get_length)
	var buf bytes.Buffer
	fmt.Fprint(&buf, "hello world")
	asciiLen := buf.Len()
	buf.Reset()
	fmt.Fprint(&buf, "你好")
	fmt.Println(asciiLen, buf.Len())
}
Output
11 6

Java v27.0.1

v27.0.1
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) {
        // ByteArrayOutputStream.size() reports the byte-buffer length (encoded as UTF-8 on write)
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        byte[] ascii = "hello world".getBytes(StandardCharsets.UTF_8);
        buffer.write(ascii, 0, ascii.length);
        int asciiLen = buffer.size();
        buffer.reset();
        byte[] cjk = "你好".getBytes(StandardCharsets.UTF_8);
        buffer.write(cjk, 0, cjk.length);
        System.out.println(asciiLen + " " + buffer.size());
    }
}
Output
11 6

JavaScript v26.13.1

v26.13.1
// JS has no output-buffering API: concatenation IS the buffer; length counts UTF-16 code units, bytes via Buffer.byteLength
let buffer = "hello world";
const asciiLen = buffer.length;
buffer = "你好";
console.log(asciiLen, buffer.length, Buffer.byteLength(buffer));
Output
11 2 6

PHP v8.6.0

v8.6.0
// ob_get_length: reports the buffer's current byte count (UTF-8 CJK counted in bytes)
ob_start();
echo "hello world";
$ascii = ob_get_length();
ob_end_clean();

ob_start();
echo "你好"; // Each UTF-8 CJK char is 3 bytes
$cjk = ob_get_length();
ob_end_clean();

echo $ascii, " ", $cjk, "\n";
Output
11 6

Python v3.15.2

v3.15.2
import io

# StringIO as the buffer; len(str) counts characters — measure bytes after encode
buf = io.StringIO()
buf.write("hello world")
ascii_len = len(buf.getvalue())
buf.close()

buf = io.StringIO()
buf.write("你好")
cjk = len(buf.getvalue().encode("utf-8"))
buf.close()

print(ascii_len, cjk)
Output
11 6

Rust v1.99.1

v1.99.1
use std::fmt::Write;

fn main() {
    // The String IS the "buffer": len() reports UTF-8 bytes, same basis as PHP
    let mut buffer = String::new();
    write!(buffer, "hello world").unwrap();
    let ascii_len = buffer.len();
    buffer.clear();
    write!(buffer, "你好").unwrap();
    println!("{} {}", ascii_len, buffer.len());
}
Output
11 6

TypeScript v26.13.1

v26.13.1
// JS has no output-buffering API: concatenation IS the buffer; length counts UTF-16 code units, bytes via Buffer.byteLength
let buffer = "hello world";
const asciiLen: number = buffer.length;
buffer = "你好";
console.log(asciiLen, buffer.length, Buffer.byteLength(buffer));
Output
11 2 6

Language differences

C: a hand-managed char array serves as the buffer; strlen counts bytes; with the source saved as UTF-8 each CJK char takes 3 bytes.

C++: str().size() on an ostringstream is the byte length; str("") empties the buffer.

C#: StringWriter's ToString().Length counts chars ("你好" is 2); PHP-style byte counts need Encoding.UTF8.GetByteCount (6).

Go: bytes.Buffer.Len() is bytes by nature — the same unit PHP uses.

Java: ByteArrayOutputStream.size() is bytes; write via getBytes(UTF_8) first, matching PHP's unit.

JavaScript: no output-buffering API; length counts UTF-16 code units ("你好" is 2) and Buffer.byteLength gives bytes (Node).

PHP: ob_get_length() reports the current buffer size in bytes while buffering is on, counting multi-byte characters bytewise; without an active buffer it returns false.

Python: io.StringIO's getvalue() yields a str and len counts characters; measure bytes by encoding to utf-8 first.

Rust: String::len() is the UTF-8 byte count — same unit as PHP.

TypeScript: same as JavaScript — length counts code units; use Buffer.byteLength for bytes under Node.