bit new to PHP I've been playing around a bit with it but I am unsure how to assign the string value of an array to a variable and print it. Currently it is only displaying the array number rather than it's data.
Any help/explanation appreciated
My current code is:
<?php
$family_friends = array();
array_push($family_friends, "James ");
array_push($family_friends, "Patrick");
array_push($family_friends, "Kevin");
array_push($family_friends, "Miles");
array_push($family_friends, "Reuben");
sort($family_friends);
// Randomly select a winner!
$winner = array_rand($family_friends, 1);
// Print the winner's name in ALL CAPS
strtoupper($winner);
echo " ". "Congratulations"." ".($winner) . "!";
?>
array_rand
returns a random index , not a random element. You need to index into the array with its return value. You also need to assign the result of strtoupper
to a variable. So:
strtoupper($winner);
Becomes:
$winner = strtoupper($family_friends[$winner]);
array_rand
returns an index, not an element. Therefore, you have to select the element of your array at the random index. Like this
strtoupper($family_friends[$winner]);
If $winner
equals zero, $family_friends[$winner]
equals "James".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.