This forum is now read-only. Please use our new forums! Go to forums
5/8 Need a correct code
I am not able to pass through this exercise. Please provideme the correct code for this.
Answer 554bc64295e3787e510005ab
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>
Answer 5539381995e37895340003d7
1 comments
also worked: $random = rand(0, strlen($name) - 1); print $name[$random];
Answer 553f6db551b88758bc000083
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);
1 comments
yes it worked for me too! thanks alot ^_^!
Answer 55436d2a51b8879d6e0002d7
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);
?>
Answer 5539f71b9113cb12b80004a7
3 comments
Well, Thanks a lot friend, it worked for me. But why did you put -1?
becose we just want to print one letter :)
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
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); ?>
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
4 comments
Thank’s amerikashka it worked for me.
thnx…
jahapana thuji great
oh….finally got the correct answer for this topic. Thanks Amerikashka