What is the best way to iterate through an array in PHP and perform specific actions based on the values?

To iterate through an array in PHP and perform specific actions based on the values, you can use a foreach loop. Within the loop, you can check each value and execute the desired actions accordingly. This allows you to efficiently process each element in the array and perform specific actions based on the values.

$array = [1, 2, 3, 4, 5];

foreach ($array as $value) {
    if ($value % 2 == 0) {
        echo $value . " is even." . PHP_EOL;
    } else {
        echo $value . " is odd." . PHP_EOL;
    }
}