Pls Explain This code
Posted: Thu Oct 06, 2011 7:06 am
Can anyone please explain the code below:
A. 312211
B. 3312212
C. 11221221
D. 221131
E. 3211122
Answer: A
Code: Select all
<?php
$string = "111221";
for($i = 0; $i < strlen($string); $i++) {
$current = $string[$i];
$count = 1;
while(isset($string[$i + $count]) && ($string[$i + $count] == $current)) $count++;
$newstring .= "$count{$current}";
$i += $count-1;
}
print $newstring;
?>A. 312211
B. 3312212
C. 11221221
D. 221131
E. 3211122
Answer: A