This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Sajal Arora
almost 9 years

5/8 Need a correct code

I am not able to pass through this exercise. Please provideme the correct code for this.

Answer 554bc64295e3787e510005ab

6 votes

Permalink

I spent much time on this excercise, though it doesn’t seem to be tough, but for some reason it put me away all the time, one of my mistakes was that i deleted comments and paragraphs from the very beginning, then i wanted to put everything in one line, but i guess the result needed middle steps, so i just want to put the whole code here which worked for me for those who are in trouble solving it! Good luck!

<html>
<p>
<?php
// Use rand() to print a random number to the screen
print rand(0,10);
?>
</p>
<p>
<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name='amerikashka';
$randomPosition=rand(0, strlen($name)-1);
$randomLetter=substr($name,$randomPosition,1);
print $randomLetter;
?>
</p>
points
Submitted by amerikashka
almost 9 years

4 comments

Pankaj Shandilya almost 9 years

Thank’s amerikashka it worked for me.

Lucky Singh over 8 years

thnx…

sai525 over 8 years

jahapana thuji great

Venkata Rajesh over 8 years

oh….finally got the correct answer for this topic. Thanks Amerikashka

Answer 5539381995e37895340003d7

5 votes

Permalink

points
Submitted by Mike
almost 9 years

1 comments

Wicdz almost 9 years

also worked: $random = rand(0, strlen($name) - 1); print $name[$random];

Answer 553f6db551b88758bc000083

2 votes

Permalink

I have done it slightly different. I always worry when my code is different to everyone else’s. haha

$name = "Matthew";
$length = strlen($name);
$random = rand(0, $length -1);
print substr($name, $random, 1);
points
Submitted by Matthew Cottham
almost 9 years

1 comments

Tahsin Tabassum almost 9 years

yes it worked for me too! thanks alot ^_^!

Answer 55436d2a51b8879d6e0002d7

1 vote

Permalink

Try refreshing your entire page/code. The code looks fine to me.

<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "Matthew";
$length = strlen($name);
$random = rand(0, $length -1);
print substr($name, $random, 1);
?>
points
Submitted by Matthew Cottham
almost 9 years

Answer 5539f71b9113cb12b80004a7

0 votes

Permalink

This code worked for me :)

points
Submitted by Ingo Dörgeloh
almost 9 years

3 comments

Sajal Arora almost 9 years

Well, Thanks a lot friend, it worked for me. But why did you put -1?

Ingo Dörgeloh almost 9 years

becose we just want to print one letter :)

Necoti almost 9 years

Hello guys, We use “-1” because, (lets say our word has 5 letters) “strlen” mesured your word as 5 letters (it goes like 1,2,3,4,5). In “substr” it starts to count your 5 letters starting from “0” (it goes like 0,1,2,3,4). As you see it end at 4 but it is 5 digit in total. So we use “-1” to exact match beween strlen lenght and substr definition.

Answer 5543b40595e378675d000262

0 votes

Permalink

Nope! I tried refrshing and erasing it with this code:<?php // Use your knowledge of strlen(), substr(), and rand() to // print a random character from your name to the screen. $name = "Matthew"; $length = strlen($name); $random = rand(0, $length -1); print substr($name, $random, 1); ?>

points
Submitted by Cameron Potter
almost 9 years