我想使用strlen(),substr()和rand()从我的名字到屏幕打印一个随机字符。
<html>
<p>
<?php
// Use strlen(), substr(), and rand() to
// print a random character from my name to the screen.
$name = "jordi";
$length = strlen("$name");
$length = rand();
$partial = substr($lengt, 0,5);
print $name. "<br />";
print $length. "<br />";
print $partial. "<br />";
?>
</p>
</html>
现在的结果(数字是随机生成的):
霍尔迪阿
9286122
92861
有人可以帮我这个。
答案 0 :(得分:4)
以下是使用作业所需的所有功能的步骤 -
$nameLength = strlen($name); // gets the length of the name
$randomNumber = rand(0, $nameLength - 1); // generates a random number no longer than the name length
$randomLetter = substr($name, $randomNumber, 1); // gets the substring of one letter based on that random number
echo($randomLetter);
答案 1 :(得分:0)
$name = "jordi";
$rndCharIndex = rand(0, strlen($name) - 1); // random character index for the string
$rndChar = $name[$rndCharIndex]; // Look up the character in that random index
echo $rndChar;