5. Math functions II


#1

$random = rand(0,4);
print ($random);

$name = "Nivaz";
$len = strlen ($name)-($random);
$char = substr($name, $random, $len);
print $char;

it gives a rand() output should be an integer error
though I get answer for a random no and random characters
I dont understand the mistake here. Can someone help me ?


#2

It must be $len-1. For me it works.:slightly_smiling:


#3

Thanks :slightly_smiling:
I added $len-1 still doesn't work.
How did you get it to run ?
Is there any other error in my code.


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

Maybe my code is wrong, but it works.


#5

Thanks much appreciated :slightly_smiling:


#6

Excuse me project_flash, but it does not work to me even I copied a whole of your code and pasted it on mine


#7

It works now, thanks alot


#8

dont understand it. It is not working.


#9

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


#10

Here's my working code:

<?php
// Use rand() to print a random number to the screen
$randednumber = rand(100, 500);
print $randednumber;
print "<br>";
?>
</p>
<p>
<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$myname = "Jaber";
//echo $myname;
//print "<br>";
$namelen = strlen($myname) - 1;
//print $namelen;
// print "<br>";
$counter = rand(0, $namelen);
$randomchar = substr($myname, $counter, 1);
echo $randomchar;
?>


#11

$n = "GodoakBrutal";
$a = substr($name, rand(0, strlen($n)-1), 1);
echo $a


#12



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



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