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

Functions, Part I Forum

View Course » View Exercise

572 points
C2a01d5c88b68d505665148cf8a7080b?s=140&d=retro
Submitted by
PiPeo
almost 2 years ago

5/8 need Help (second part)

<?php $name = "pipeo";
nameLen = strlen($name);
print substr($name, rand(0, $nameLen)-1, 1); ?>

Error: "The random output you printed from your name should be just one character"


2 votes

permalink

Like Christopher says, this

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

Works great and let me go to the next section. If you have still the error clear cache etc. from browser and check again

1409 points
588b2ac3e1281365f0000133_918398059
Submitted by
Codmakr
almost 2 years ago

1 Comment

563f564eb62548d5c9000854_286301216 Darkhorse2 almost 2 years ago

Thanks for this.
Based on my knowledge of the previous lessons, I would never have worked this out as a newbie programmer.


0 votes

permalink

<?php $name = "pipeo";
nameLen = strlen($name);
print substr($name, rand(0, $nameLen)-1, 1); ?>

<?php
$name = "pipeo";
$nameLen = strlen($name);
print substr($name, rand(0, $nameLen - 1), 1);
?>

All you need to add is the $ before the nameLen variable.
It clears perfectly on mine.

1111 points
Picture
Submitted by
Christopher Wright
almost 2 years ago

6 Comments

C2a01d5c88b68d505665148cf8a7080b?s=140&d=retro PiPeo almost 2 years ago

It doesn't work (T.T) ty anyway <3

56123acce39efe544500019e_572520967 Elena almost 2 years ago

Here it works:

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


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

Picture Stefan de Jong almost 2 years ago

Sadly no solution seems to work, this course seems broken :( tried all options people said worked for them and still getting the "output from rand() should be an integer" (which it is)

588b2ac3e1281365f0000133_918398059 Codmakr almost 2 years ago

Stefan please show your code here

56123acce39efe544500019e_572520967 Elena almost 2 years ago

Stefan in php coding sometimes refresh window with code. Here when i type slowly it gives me that here is mistakes

Picture Stefan de Jong almost 2 years ago

DooKoo.de sadly my code is now gone through all my testing but even having only "print rand(1,10); will give the integer error ... its just very silly.

Original program:
<?php
print rand();
echo ""
?>

<?php
$name = "Stefan";
$length = rand(1, strlen($name)-1);
print substr($name, $length, 1);
?>

Elena: trust me when I say that I tried everything and anything when it comes to refreshing, keeping code in page, copying it to refreshed page, etc. it is simply a silly bug.

I've finished the whole php course except for this one silly task


0 votes

permalink

Guys look:
<?php
// Use rand() to print a random number to the screen
print rand();
?>

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

$length = strlen($name); // for my mane it gave 5
print substr($name, rand(0, $length)-1, 1);
?>

What we have done - gave to variable our string, with teg strlen() it gave how many letters do we have. Then we need to have only one letter from our name. So we need to use rand(0, $length)-1 instead of we use before substr($name, 0, 1); 0 - "H" and 1 - "e".

707 points
56123acce39efe544500019e_572520967
Submitted by
Elena
almost 2 years ago