Math Functions II


#1


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


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


<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.
    $name = "Mark Karnaukh";
    $length = strlen($name);
    print "<p>$length</p>";
    
    $randomLength = rand(0, 12);
    $partialString = substr($name, 0, $randomLength);
    print "<p>$partialString</p>";
    print $name{$randomLength};
    
    ?>
    </p>
</html>


#2

The syntax for substr is substr($inputstring, $startposition, $length). This exercise is asking for one single random character to be outputted from your name. Currently you have the length portion of substr() set to be a random number, this will result in a string anywhere from 0 to 12 being outputted.


#3

Thanks for help, it works now!


#4

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