Use your knowledge of strlen(), substr(), and rand()


#1




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



<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "SUNG EUNHYE";
$max = strlen($name);
$one = rand(0,$max-1);
print substr($name,$one,$one-1);
?>




01. Use the editor to print a random number.
02. Create a new variable $name and store your name in it.
03. Then print a random character from your name. Use your knowledge of strlen(string), rand(min, max), and substr(string, start, length) to do this.

HINT: Remember that substr() treats characters in a string as a zero-indexed array (first letter is at position zero). This means that the last character in the string will be at position length - 1.


Replace this line with your code.


#2

As the third parameter gives the length of the substring you can use 1
like
print substr($name,$one,1);

http://php.net/manual/en/function.substr.php


#3

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