Math Functions II


#1

This is my code



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

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

it keeps telling me that my rand() should return an integer. What am I doing wrong?


#2

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

You've left this part empty, completely ignored it.
You should call rand() with some interval in it and you will most likely pass then.
Why you made so many variables? You could have called strlen() and rand() directly inside substr(). I'm not criticizing, just curious.


#3

There are 2 php tags in this program. You have make the changes in the first php tag.


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



<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
?>


#4

print rand();
$name = "Flasher";
$len = strlen($name);
$rand = rand(0, $len-1);
print substr($name, $rand, 1);


#5

For me I am always trying to find the smallest piece of code. The instructions explains and hint you to create one variable, not three. However, whatever works right :slightly_smiling:

Here is my take on it:

<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.

$name = 'Paulo';
echo substr($name, rand(0, strlen($name) - 1), 1);

?>

#6

My code here:


<?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 = "Felice";
print $hihi = substr($name,5,rand(1,strlen($name)-1));

?>
</p>


#8

I finished every other unit of PHP about 8 months ago, maybe more except for this one exercise. My code looks like:

print rand();
print ("<br>");
$name="Travis";
$length=strlen($name);
$rand=rand(0,$length-1);
print substr($name, $rand, 1);

Which prints out a random number and a random letter from my name.
But I always get this error: "Oops, try again. Your printed output from rand() should be an integer." Why? I'm confused, I don't even know what they want.


#9

Great code! I'm sure that is what they were "expecting". The best solution to the problem so far.


#10

@tfantina

If you have a problem, please post this in a new topic with your question+ error messages + code