I do not understand


#1

on the math functions ii, how do you make it print a random number from the length of your name?


#2

Show us please how your code looks now!


#3

Try this here see if you can understand it. Add try your own solution afterwards

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

    $string ="Yourname";
    
    //what is the number generated?
    switch(rand(1,10)):

        //if < 6
        //then just get the first character in name

        case rand(1,10) < 6 :
            echo substr($string,0,1);
            break;

        //if >5
        //randomly select the second or third

        case rand(1, 10) > 5 :
            echo substr($string,rand(1,2),rand(1,2));
            break;

        //if none of the conditions are met then
        // do this

        default:
            echo "this is not happening";
    endswitch;
    ?>

#4

Rydan This is not a true random letter from a name string example and I will explain why.
If you're giving an if command to a certain outcome which in this case is the random number then you're cheating the system because depending on the outcome, only certain cases will run. so unless you write an endless code of cases to every random number like between 0, 1 between 1,2 until it reaches your name length.etc then you truly have a random letter from a name string. So why not create a variable of our name like you did, then create a variable of a random number which will be the length of your name my name is murat so it will be 0,4 get it cause our number will start at 0? so here is the code

<?php
    // Use your knowledge of strlen(), substr(), and rand() to
    // print a random character from your name to the screen.
$name = "murat";
$randomnumber = rand(0,4);
$kimbilir = substr($name, $randomnumber, 1); // here is where your random is telling substring from 0,4 which letter is to count, where 1 is telling the substring only to grab 1 letter
echo $kimbilir;
    ?>

#5

And where do you use strlen()?

:smile:


#6

You dont have to , you're already using a random number that is equal to the string lenth of your name which in my case murat = 5 which is rand(0,4)


#7

And if you now want to change the name in maybe Lysander you have to count everytime the length manually?! Bad Idea!

And in addition the requirement is:

Use your knowledge of strlen(), substr(), and rand() to


#8
And if you now want to change the name in maybe Lysander you have to count everytime the length manually?! Bad Idea!

you're killing me bro here is your code

<?php
$name = "murat";
$blablabla = strlen($name);
$numberrandom = $blablabla - 1;
$randomnumber = rand(0,$numberrandom);
$kimbilir = substr($name, $randomnumber, 1);
echo $kimbilir;
    ?>

since our number starts at zero we have to put a math function which will minus 1 at the end so we take that variable and convert it to random number which will still equal to 4 that's the goal. With this code you don't have to count the name by manually. Hope you're happy :grin: I'm killing it for a beginner tho


#9

This is good, however I do not believe that you need this variable: $numberrandom. You could do $some_var = strlen($name) - 1;


#10

thank you! I would never have thought about.


#11

you're right its an extra variable, good call