Where is the mistake?


// Use rand() to print a random number to the screen
print rand(5,6582);

// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "sona";
print rand(substr($name,strlen($name)-1,3),3);


Please help me with my coding. I do not really understand what the heck with this code.




The substr() function takes three arguments...

substr( $string, $start_index, $length )

For the string, we use our name in this exercise. The start will be a random number, and the length will be 1. So this means rand() will be inside the substr argument list.

substr( "Wee Gillis", rand(), 1)

Now for a range to use in the rand() arguments. The function takes two of them,

rand( $min, $max)

The min will be the starting index of the string, 0, and the max will be the string length minus 1. So,

rand(0, strlen("Wee Gillis") - 1)

Substituting this into our substr expression above should give us a working expression.


Thanks a lot :slight_smile: i am very grateful


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.