Solution:
In php, strings do not end with a null byte. For example, $s = 'a';echo $s[1];
produces a warning (that’s why you shouldn’t test with $a[$length] == ""
). Also, php strings can contain null bytes – they’re really byte arrays.
However, you can use the language construct isset
to test whether reading the value of $a[$length]
would produce a warning:
$a = "a\0b\0c";
for ($length = 0;isset($a[$length]);$length++) ;
echo $length; // 5