How to count the characters of a string in PHP

<?php
$string = 'hello 0';

for($i = 0; @$string[$i] || @$string[$i] == '0'; $i++); /* $string[$i] echoes an error message when $i = 7 and  @ (error control operator) hides that error. When the error occurs $string[$i] = Null because $i is undefined index number. Second condition is used here to treat '0' as a true value */

echo $i; // outputs 7
?>

Comments

Popular posts from this blog

Timus Problem 1086. Cryptography Accepted Solution in C

Timus 1209. 1, 10, 100, 1000... accepted solution in C

Timus 1083. Factorials!!! Accepted Solution in C