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

Functions, Part I Forum

View Course

306 points
7c5895a96f13b895eb498e7b6faa0729?s=140&d=retro
Submitted by
nicoco59
over 1 year ago

PHP, Functions 5/8 Math Functions II, Bug?

Can someone tell me what's wrong in my code ? I always get this error :

Oops, try again. Your printed output from rand() should be an integer.

The rand() function is already an integer, so I don't understand what's wrong...

Thanks

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


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


1 vote

permalink

HI,

i hope i can explain it well:

subtring counts a the string from zero (zero-indexed array) !

In my case $name = "David" substring counts D=0; a=1; v=2; i=3; d=4;
And that means:
It could be that the result from rand(0,$nameLength) is 5
Because $nameLength = strlen($name) // Result 5;
What result give you substr($name, 5,1); now ?
Answer is False or " " !

I hope you understand my explanation.

BR
D.

1409 points
588b2ac3e1281365f0000133_918398059
Submitted by
Codmakr
over 1 year ago

2 Comments

88d2becae9279dbda0e5514fa89ee9c0?s=140&d=retro Pat over 1 year ago

rand(0,$nameLength) gave me undefined variable...

588b2ac3e1281365f0000133_918398059 Codmakr over 1 year ago

$name = 5;
$nameLength = strlen($name);
rand(0,$nameLength);

This should work


0 votes

permalink

Store your first random number in a variable and then print it. That'll fix your problem.

777 points
531dbda552f8631658008642_454003526
Submitted by
Kevin
over 1 year ago


0 votes

permalink

No this dowsnt fix the issue!

First of all look into the manual
There you can see how substr works.
And rember the Instructions:

Then print a random character

1409 points
588b2ac3e1281365f0000133_918398059
Submitted by
Codmakr
over 1 year ago


0 votes

permalink

O meu eu fiz assim e funcionou

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

$name = "Fernando";
print rand(1,10);
?>


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

$longueurNom = strlen($name)-1;
print substr($name, 0, rand(1,$longueurNom));
?>

91 points
7a01f3739b999d45a2b49d8730322761?s=140&d=retro
Submitted by
Fernando Dantas De Sousa Junior
over 1 year ago

1 Comment

588b2ac3e1281365f0000133_918398059 Codmakr over 1 year ago

You didnt can complete the Section with this Code:

Oops, try again. The random output you printed from your name should be just one character.


0 votes

permalink

A character means one character.

So your Code is wrong because

substr ( string $string here $name, int $start this must be a randome number [, int $length 1 because we want one character ] )

1409 points
588b2ac3e1281365f0000133_918398059
Submitted by
Codmakr
over 1 year ago

2 Comments

8214e1adbf60ba3b2a1b4589cf3fe7f5?s=140&d=retro zahra shahsavari over 1 year ago

please, write cleary

588b2ac3e1281365f0000133_918398059 Codmakr over 1 year ago

Read the manual there you can read how the function substr() works

http://php.net/manual/en/function.substr.php

And then read again the instructions.


0 votes

permalink

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

$name="Michael";
print rand();

?>


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

$nameLength = strlen($name)-1;
print substr($name, rand(0,$nameLength),1); //remember the order that substr takes its arguments : string, starting position, and number of characters to return.

?>

This worked for me, although i'm still a little unclear on why we put $name-1 for the length, could someone explain that? I understand the 0 based indexing, but it just doesn't make sense to me.

804 points
Fbe3eff50d8af77947d0170dea56d2b0?s=140&d=retro
Submitted by
Zeddicus91
over 1 year ago

2 Comments

88d2becae9279dbda0e5514fa89ee9c0?s=140&d=retro Pat over 1 year ago

$name="Michael";
print rand();
not working...

Picture Stefan de Jong over 1 year ago

Explanation of why -1 is that substr and other function work by counting from 0 up, so your namelength is 6 because it starts counting at 0. Thus -1 will make sure it doesn't try to count 8


0 votes

permalink

<?php
print substr("justaname",0,1);
?>


Use this just to trick the lesson :(
(no idea how to solve that)

325 points
Picture
Submitted by
alexvalydevelop
over 1 year ago


0 votes

permalink

Okay for all the Solution from this Section:

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

1409 points
588b2ac3e1281365f0000133_918398059
Submitted by
Codmakr
over 1 year ago


0 votes

permalink

Both solutions above do not work. I believe there simply is a bug in how it looks at code.

I'm stuck at 99% of PHP course due to this.

Dear CodeAcademy PLEASE fix this issue!

526 points
Picture
Submitted by
Stefan de Jong
over 1 year ago