# 5. Math Functions II

#1

Hi!
i make this code but i have the problem the error and i don't know what is it

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

// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
\$name = "santiago";
\$strlen = strlen(\$name-1);
\$random = rand(0,\$strlen);
\$substring = substr(\$name,\$random,1);
echo \$substring;

?>

#2

You cannot put a variable for a parameter for the function rand().

This is my code, that had passed:

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

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

\$name = "Devon";

``````\$subname = substr(\$name, 0, 3);
\$namelen = strlen(\$subname);
\$randomized = rand(1,3);

switch(\$randomized):

case 1: echo "D";
break;

case 2: echo "e";
break;

case 3: echo "v";
break;

default: echo"ERROR";
endswitch;

?>
</p>``````

There's probably an easier way to achieve the same results, but this is my solution.

#3

no your code is correct but i have this exercise again :S i have other code is this

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

<?php

\$name = "santiago";
\$length = strlen(\$name);
\$random = rand (0, \$length-1);
\$char = substr(\$name, \$random, 1);
print \$char; ?>

But de error is not safe helppp

#4

Dont over complicate things. What they're stating in the Instructions is that there are multiple ways of being able to complete this task using the StringLength (strlen), Random (rand) and SubString (substr).

From this there is a very simple way to be able to complete this task as shown below.

`

``````<?php

\$subname = substr("Name", -1);
echo \$subname;

?>``````

Dont overcomplicate things.

#5

How about using rand() command !? Can you give me the solution using rand ?

#6

Yes, it would be nice to see an example with rand(). I did successfully complete lesson fashioning @redoxau's example . Commenting to keep alive the thread :-).

#7

``````\$name = "myname";