Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Strings and text encoding

PHP strings are byte sequences and may contain a zero byte. strlen counts bytes and a string offset selects one byte.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

strlen($text); $text[0];

PHP strings are byte sequences and may contain a zero byte. strlen counts bytes and a string offset selects one byte. In UTF-8 a character can occupy several bytes. Splitting a string at an arbitrary byte boundary can therefore yield an invalid encoded fragment.

$s="é\0A";echo json_encode([strlen($s),bin2hex($s[0]),bin2hex($s[1]),bin2hex($s[2])]);
[4,"c3","a9","00"]

Examples

<?php
$s="é\0A";echo json_encode([strlen($s),bin2hex($s[0]),bin2hex($s[1]),bin2hex($s[2])]);

Result

[4,"c3","a9","00"]

Sources