What is the purpose of using a for loop with an if statement in PHP?
Using a for loop with an if statement in PHP allows you to iterate over a set of data and conditionally execute a block of code based on a specific condition. This is useful when you need to perform a certain action only on specific elements of an array or when you want to filter out certain values. By combining a for loop with an if statement, you can efficiently process data and make decisions based on the values being iterated.
// Example of using a for loop with an if statement in PHP
$data = [1, 2, 3, 4, 5];
for ($i = 0; $i < count($data); $i++) {
if ($data[$i] % 2 == 0) {
echo $data[$i] . " is even" . PHP_EOL;
}
}