Hello everyone, I am fairly new to PHP and I am trying to solve this problem where I need to make a for loop to generate 10 random numbers from the range (1,20) and then output whether they are odd or even.

I am so stuck on this though. So far I have this below, but I know its wrong somewhere. Any help or guidance would be much appreciated.

$rand120 = rand(1, 20);
for($rand120 = 10; $rand120 <= 20; $rand120++);
{
echo "$rand120"."<br/>";
}
if ($rand120 % 2 === 0) {
echo "$rand120 is even </br>";
} else {
echo "$rand120 is odd </br>";
}

^^ is correct but ill explain a little why yours is wrong. Youre assigning $rand120 a random number which is good but its outside of your loop so its only getting the value once when you need 10 of them also youre overwriting the random number with your for statement

The first line giving it lets say a value of 17 and the the second line for(...) gives it 10 no matter what.
In the above solution a loop using a counter variable $i is what you were looking for its generally what you will use if you want to run through something X amount of times (you want 10 numbers)

Your If Else statement is decent but it would need to be inside the loop like in the above example because you want to check each of the ten numbers for odd or even, === isnt really needed either == is fine.

Hopefully I helped explain Peak's solution a little.

<?php
// Create an array of numbers from 0 to 20
$numbers = range(0, 20);

// Randomized the array
shuffle($numbers);

// Slice the first 10 elements from the array
$numbers = array_slice($numbers, 0 , 10);

// Transverse over the sliced array checking if the number is odd or even :-)
foreach($numbers as $number) {
$output = ($number % 2 == 0) ? "even number" : "odd number";
echo $number . " is an " . $output . PHP_EOL;
}

2.
- You can also use range(), to create an array of numbers from 0 to X where X is a number of random numbers.

- Use array_map to randomized each element. So you will have a function: randomizedCallBack(x, y) where x is the lowest bound and y is the highest bound, that returns a random element (rand(x, y)).

- Now you have an array of randomized numbers. Then run the foreach loop and check if the number is odd or even.