Whats wrong with my code? Please Help


#1



https://www.codecademy.com/courses/web-beginner-en-7I7Xm/0/8?curriculum_id=5124ef4c78d510dd89003eb8#


3Mohit,Rohan,Sham1


<html>
    <p>
    <?php
    // Create an array and push on the names
    // of your closest family and friends
       $family = array();
    array_push($family,"Rohan");
    array_push($family,"Sham");
    array_push($family,"Mohit");

    // Sort the list
print count($family);
sort($family);
print join (",",$family);

    // Randomly select a winner!
$random = rand(0,count($family) - 1);

    // Print the winner's name in ALL CAPS
    print strtoupper($random);
    ?>
    </p>
</html>


#2

Hello once again @rohit_786 :stuck_out_tongue:

Why you want to print your array two times, when it's not neccessary? :rolling_eyes:
Well, that doesn't matter right now.

Your variable $random contains a integer, so you can't simply pass it to strtoupper();.
First, you have to pick a $winner (for example) out of your array $family by accessing the array ($family) at the position which your $random-variable has.

So, in your case, just add $winner = $family[$random]; right under the line in which you declare the $random-variable, then - at your print-call - replace $random with $winner and it should work!

Hope that I could help you :slight_smile:


Your code should look like this then:

<html>
    <p>
    <?php
    // Create an array and push on the names
    // of your closest family and friends
    $family = array();
    array_push($family,"Rohan");
    array_push($family,"Sham");
    array_push($family,"Mohit");

    // Sort the list [ADDED: You don't need to print them..]
    // print count($family);
    // print join (",",$family);
    sort($family);

    // Randomly select a winner!
    $random = rand(0,count($family) - 1);
    $winner = $family[$random];

    // Print the winner's name in ALL CAPS
    print strtoupper($winner);
    ?>
    </p>
</html>

#3

Thanks a lot once again . I way you make things easy to understand is really great thing.


#4

can you please explain me this line of code . What we have done here?


#5

Well, glad to read that :slight_smile: As I said in my Introduction, I'm always happy to help the people :smiley:


#6

I'd also like to understand the logic behind this code and how adding $random in [] after $family retrieves the actual name and not integer.