printing out 1 random letter of a word php functions [duplicate]

[亡魂溺海] 提交于 2020-01-14 06:29:10

问题


i want to Use strlen(), substr(), and rand() to print a random character from my name to the screen.

<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>

the outcomes right now(the numbers are randomly generated ofcourse):

jordi

9286122

92861

can someone help me with this.


回答1:


Here are the steps using all of the functions required by your assignment -

$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);



回答2:


$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;


来源:https://stackoverflow.com/questions/23915813/printing-out-1-random-letter-of-a-word-php-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!