This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Functions, Part I Forum

View Course » View Exercise

917 points
Picture
Submitted by
Jeff Cunningham
over 2 years ago

Help with my code.

What do you guys think of this? how can I tweek it to print just one letter?

$name = "jeff";
$x = strlen($name);
$i = rand(0, 4);

print substr($name, $i, $x);


1 vote

permalink

$name="jeff";
print substr($name, rand(0, strlen($name)-1), 1);

Or simply one line code:
print substr("jeff", rand(0, strlen("jeff")-1), 1);

905 points
555ffe9676b8fe7f51000634_734380485
Submitted by
Sprintingsnail
over 2 years ago

1 Comment

Picture Jeff Cunningham over 2 years ago

Thanks, I ended up using: $name = "jeff";
$x = rand(0, 4);

echo $name[$x];


1 vote

permalink

Here is my solution

$name = "Ahmad";
$length = strlen("Ahmad");
$newlength = $length - 1;
$random = rand(0, $newlength);
print substr($name, $random, 1);

545 points
283e4cfe624216454c4940c1108e1605?s=140&d=retro
Submitted by
Hero0o
over 2 years ago


0 votes

permalink

$name = "chris";
$len = strlen($name);

$ran = rand(0,$len);

$randomletter = substr($name,$ran,1);
print $randomletter;

I like that one line code above, that's better than the long way i came up with.

813 points
4987b2d01556f0a9cd86f90c2f30fef2?s=140&d=retro
Submitted by
Chris Conner
over 2 years ago