Php name


#1


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

i can't understand ?

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

    ?>
    </p>
</html>


#2

Let's set out some working variables:

    $m = "name";
    $n = strlen($m);
    $r = rand(1,$n);
    $c = substr($m, $r -1, 1);

Everything to the right of the equals sign is an expression. All four of them. Each building on the one before it, and resolving in the final expression.

$m is self evident. It is a string expression.

$n is the string length. We know that it is 1 more than we need it, but I like to resolve things rather than mutate the output of a pure function.

$r is random number between 1 and $n, inclusive. I start with 1 because I don't want the subtraction later to give a negative 1. Again, I want a pure result, not one I've mutated.

With 1 and $n both being offset by 1, we're just shifting the whole range over by one. This is what takes place in the final expression, $c, the $r - 1 expression..

This can be written into a single echo statement. Consider,

print substr($m, $r - 1, 1);

substituting for $r,

print substr($m, rand(1, $n) - 1, 1);

and now substituting for $n,

print substr($m, rand(1, strlen($m)) - 1, 1);

and if we choose to go the distance, substitute for $m:

print substr("name", rand(1, strlen("name")) - 1, 1);

Now we have no variables, only the expressions they represented.

This example is not meant as a solution for you to pass. If any of the concepts are still fuzzy, go back and play around in the lessons for each function in this exercise. Be comfortable with them because they will be on call a lot.