# 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

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.

Pat almost 2 years ago

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

Codmakr almost 2 years ago

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

This should work

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

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

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));
?>

##### 1 Comment

Codmakr almost 2 years ago

You didnt can complete the Section with this Code:

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 ] )

zahra shahsavari almost 2 years ago

Codmakr almost 2 years 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.

<?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.

Pat almost 2 years ago

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

Stefan de Jong almost 2 years 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

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

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

Okay for all the Solution from this Section:

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