php - rand() to slelect array elements

829

i would like to Create an array() with the names of your closest family and friends.

After i've created my list, i need to sort it and randomly select a name from the list.

When you have your winning name, print it to the screen in caps so everyone knows how awesome the winner is.

Functions i'd like to use here: array(), array_push(), sort(), count(), rand(), and strtoupper(). code now:

<html>
    <p>
    <?php
    // Create an array and push on the names
    // of your closest family and friends
    $family = array("jan","klaas","oma");
    // Sort the list
    sort($family);
    join(", ", $family);
    // Randomly select a winner!
   $family rand(0, 2);
    // Print the winner's name in ALL CAPS
    ?>
    </p>
</html>
222

Answer

Solution:

since you need specific functions to be used here is the answer.

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

    // Sort the list
    sort($family);
    $count = count($family);

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


    // Print the winner's name in ALL CAPS
    echo strtoupper($family[$selected]);
    ?>
    </p>
</html>

People are also looking for solutions to the problem: php - What can trigger an Ajax 404 error on url that exists... and works?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.