What is the correct way to check if values in an array fall within a specific range in PHP?

To check if values in an array fall within a specific range in PHP, you can use a foreach loop to iterate through the array and use conditional statements to check if each value falls within the specified range. You can then store the values that meet the criteria in a new array or perform any other desired actions.

<?php
// Define the array of values
$values = [10, 20, 30, 40, 50];

// Define the range
$min = 20;
$max = 40;

// Initialize an empty array to store values within the range
$valuesInRange = [];

// Check if values fall within the range
foreach ($values as $value) {
    if ($value >= $min && $value <= $max) {
        $valuesInRange[] = $value;
    }
}

// Output the values within the range
print_r($valuesInRange);
?>