# Need Explanation for 5/8

I got the code for this exercise from the forum itself, but I can't understand what's happening at line

<?php
// Use rand() to print a random number to the screen
print rand(0,10);
?>

<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
\$name='luckysingh';
\$randomPosition=rand(0, strlen(\$name)-1);
\$randomLetter=substr(\$name,\$randomPosition,1);

print \$randomLetter;
?>

Can you please explain me those highlighted lines in the above code. I really don't get it.

\$name="naib hossain khan";
print \$name[rand(0,strlen(substr(\$name,0,strlen(\$name))))];

\$name = 'Andy';
\$length = strlen(\$name);

``````\$namelength = rand(0,\$length);

\$namelength = \$namelength - 1;

\$solution = substr(\$name,\$namelength,1);
echo \$solution;
``````

well i did it this way which is a bit less complex than the way you did it hopefully you will be able to figure it out when you look at this.

Lucky Singh almost 3 years ago

Thanks bro, your code was pretty easy to understand. Thanks!! :D

Andrew Dear almost 3 years ago

np :)

Oskar Le almost 3 years ago

thanks for the codes. I got mad and just typed print "a"; and passed....

ColorfulPastels almost 3 years ago

Thanks bro. You helped me a bunch.

1 vote

<?php
// Use rand() to print a random number to the screen
print rand (1,10)
?>

<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
\$name = "Ben";
\$length = strlen(\$name);

\$namelength = rand(0,\$length);

\$solution = substr (\$name, \$namelength,1);
echo \$solution;
?>