strlen

strlen(string $string): int

Returns the byte length of a string.

C C23

C23
#include <stdio.h>
#include <string.h>
int main(void) {
    printf("%zu\n", strlen("hello")); // byte count (stops at the NUL terminator)
    printf("%zu\n", strlen("你好")); // UTF-8 bytes: 6; the stdlib has no character-count API
    return 0;
}
Output
5
6

C++ C++26

C++26
#include <iostream>
#include <string>
int main() {
    std::string s = "hello";
    std::cout << s.size() << "\n"; // std::string length is the byte count
    std::cout << std::string("你好").size() << "\n"; // UTF-8 bytes: 6; no character-count API in the stdlib
    return 0;
}
Output
5
6

C# v11.0.100

v11.0.100
Console.WriteLine("hello".Length);
// .NET strings are UTF-16 internally: Length counts UTF-16 code units
Console.WriteLine("你好".Length); // 2; Encoding.UTF8.GetByteCount gives 6
Console.WriteLine(System.Text.Encoding.UTF8.GetByteCount("你好"));
Output
5
2
6

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
    fmt.Println(len("hello"))

	// len() counts bytes (6); a range loop walks runes (2 characters)
	n := 0
	for range "你好" {
		n++
	}
	fmt.Println(len("你好"), n)
}
Output
5
6 2

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        System.out.println("hello".length());
        // UTF-16 internally: length() counts code units; getBytes(UTF_8).length gives bytes
        System.out.println("你好".length());
        System.out.println("你好".getBytes(java.nio.charset.StandardCharsets.UTF_8).length);
    }
}
Output
5
2
6

JavaScript v26.13.1

v26.13.1
console.log("hello".length)
// UTF-16 code units: a 2-char CJK string has .length 2; Buffer.byteLength gives UTF-8 bytes (6)
console.log("你好".length)
console.log(Buffer.byteLength("你好"))
Output
5
2
6

PHP v8.6.0

v8.6.0
echo strlen("hello"), "\n";
// bytes vs characters: strlen counts bytes (3+3=6), mb_strlen counts characters
echo strlen("你好"), " vs ", mb_strlen("你好");
Output
5
6 vs 2

Python v3.15.2

v3.15.2
print(len("hello"))
print(len("你好"))  # len counts characters (2)
print(len("你好".encode("utf-8")))  # encode() then len counts bytes: 6
Output
5
2
6

Rust v1.99.1

v1.99.1
fn main() {
    // len() returns the UTF-8 byte count, consistent with PHP strlen (not the character count for non-ASCII)
    println!("{}", "hello".len());

    println!("{}", "你好".len()); // UTF-8 bytes: 6
    println!("{}", "你好".chars().count()); // characters: 2
}
Output
5
6
2

TypeScript v26.13.1

v26.13.1
const s: string = "hello";
console.log(s.length);
console.log("你好".length); // UTF-16 code units: 2
console.log(Buffer.byteLength("你好")); // UTF-8 bytes: 6
Output
5
2
6

Language differences

C: counts bytes with strlen() (it stops at the NUL terminator).

Go: uses len(); it also returns bytes.

Java: the .length() method (counts UTF-16 code units).

JavaScript: the .length property (counts UTF-16 code units).

PHP: strlen returns bytes, not characters (use mb_strlen for multibyte text); the counting unit differs by language — PHP/Go count bytes, Python counts Unicode code points, JS/Java count UTF-16 code units; this entry demonstrates the gap with the UTF-8 string "你好": 6 under byte counting, 2 under character counting.

Python: uses len() (counts Unicode code points).

Rust: len() returns UTF-8 bytes, consistent with PHP strlen; use chars().count() for the character count.