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
?>
$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
Post a Comment