Math Functions II


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

    // Use rand() to print a random number to the screen
    print rand();
    // 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};


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.


Thanks for help, it works now!


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