Function Part 1 (5. Math Functions 2) Please help!


#1

https://www.codecademy.com/courses/web-beginner-en-7I7Xm/0/5?curriculum_id=5124ef4c78d510dd89003eb8

Please help. The code keeps on sending me an error message which reads "Oops, try again. The random output you printed from your name should be a letter."

    <p>
    <?php
    $name = "Siphiwe";
    $length = strlen($name)-1;
        echo $name[rand(0,$length)];
    ?>
    </p>

#2

Meaning the random number needs to be in range. $length is outside of that range. Subtract 1 from the length.


#3

Hey @mtf,

Thank you for your reply and assistance. I worked on my code and I'm still struggling with this question. What am I missing?

Please have a look at it on: https://www.codecademy.com/courses/web-beginner-en-7I7Xm/0/5?curriculum_id=5124ef4c78d510dd89003eb8

Now the error message reads: "Oops, try again. The random output you printed from your name should be just one character."

This is the part of code i'm referring to:


<?php
$name = "Siphiwe";
$length = strlen("Siphiwe")-1;
$name[rand(0,2)];
$partial = substr($name,0,2);
echo $partial;
?>

And then I tried:


<?php
$name = "Siphiwe";
$length = strlen("Siphiwe")-1;
$name[rand(0,2)];
$partial = substr($name,$length-1);
echo $partial;
?>

What am I missing?


#4

This is transient and does not get preserved anywhere. Sssphhuutt!. Gone. It is not a statement, but an expression. If PHP doesn't snag on it, it will ignore it.

You were closest at the beginning when things were more explicit. One does not want to get caught up in monitoring too many variables.

Going for the meat of the matter, straight off...

$name = "Siphiwe";
echo substr($name, rand(0, strlen($name) - 1), 1);

Some people find variables to be useful; others find functions useful in place of variables.

By limiting the number of variables we declare in a particular operation, we limit the number of objects we need to monitor in a dynamic setting. This is vital. Less is more.


#5

Wow!

Thank you for your assistance. I will remember to limit the number of variables I declare. I hate the feeling of being stuck, I appreciate your assistance.

Thank you


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.