I just need a little help Math Functions II 5.7


#1



<?php
print rand(1,20);
?>
<?php
$name ="Jackie Thomas";
$i = strlen($name);
echo $i;
?>


$name = "Jackie";
$strlen = strlen($name); -1
$rand2 = rand(0, $strlen - 1); // from 0 to 6
$substr = substr($name, $rand2, 1);
print $substr;
?>


this is the message I get so I know I 'm close
Oops, try again. The random output you printed from your name should be just one character.


#2
<?php

print rand();
print rand ( 1, 14);
?>

</p>
<p>

<?php
$name ="Andrei";
  $partial = substr ($name, 0,1);
    echo $partial;
?>

This worked for me. I hope it's ok.


#3

thank you it seems right but this is the error I received

Oops, try again. The random output you printed from your name should be just one character.

it is just 1 character with a question mark j ?


#4

Thank you so much finally It worked for me I have completed PHP


#5

ladyjaz how did you do it?


#6

substr($name,rand(0,strlen($name) - 1),1 );


#7



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


<?php
$name ="cecsily";
$partial = substr($name,0,1);
echo $partial;

?>
</p>

there was just an extra ?>


#8

I don`t know why it doesn't work..



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



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


The code printed out:

7
ergi

The system gives me a mistake :

Oops, try again. The random output you printed from your name should be just one character.

Could anybody help me?


#9

Thanks, I got it myself/

The mistake is '4'. Should be just '1'.

:smile:


#10

This is how I did it, using what the told us to use. I hope it helps.


$name = "Sninchak";

$g = strlen($name); //8 -1 = 7

$h = rand(0, 7);//echo gave me 5. deleated echo

$i = substr($name, 5,1);//start at position 5, echo 1 letter, h

echo $i

#11

The prompt for the third question says to print a random character from your name. The code you have looks like it would just print "A" every time.

This is what I did to get a random letter:

$name = "luke";
$nameLen = strlen($name);
$letterPos = rand(1,$nameLen);
$letter = substr($name, $letterPos - 1, 1);
echo $letter;


#12

Hello @thefiction The solution posted by you was indeed helpful.
Basically, we need to make use of the rand() function before making use of the substr() function in the referred example.
Thank you.


#13

OMG thanks! your code saved me :smiley:


#14

it`s great! i think you'd get it yourself.


#15

This response best fits with the prompt and the "help" tips, which ask you to use the substr, rand, and strlen functions.

The prompt asks you to print a random letter from your name. The string, then, is your name. We need a substring of that, to get just one letter.

So, we know we need to echo/print substr.

A substring needs to know which string you're taking from (in this case, your name), where you want to start taking within the string, and how many characters to return.

We know the string we're taking from (our name) and how many characters we need (just 1 letter). So, where do we start within the string? That's where random and stringlength functions come in.

We're telling the substring that we're pulling randomly within the string, but the random function needs to know where to start and where to end. We're telling it to start at 0 in the array, or the first letter in our name/string. We're telling it to end at the last letter, using the stringlength function; but, because it's an array, the last letter in our name/string is represented as -1.

So, we get print substr($name,rand(0,strlen($name) - 1),1);

I hope this clarifies how this works (typing it out sure helped me understand). Thanks, chippro!


#16

i used the exact code but it's not working! :frowning:


#17

try it


<?php
// Try rounding a floating point number to an integer
// and print it to the screen
$round=round(1,3);
print $round;

?>
</p>
<p>
<?php
// Try rounding a floating point number to 3 decimal places
// and print it to the screen

$name ="Andrei";
$r=rand(0,strlen($name)-1);
$partial = substr ($name, $r,1);
echo $partial;

?>
</p>


#18

thefiction is right, I just fixed this by replacing "5" in $i $h, that way it is a truly random value, not just 1 random value that you echoed

$g = strlen($name);

$h = rand(0, 7);

$i = substr($name, $h,1);

echo $i