Deep Tech Point
first stop in your tech adventure

Can you use two conditions in a for loop in PHP?

March 1, 2022 | PHP

In this article, we are going to learn about the for loop in PHP – we are going to look at the for loop syntax through a simple example. In the end, we will learn what can we do if we want to introduce two conditions in a for loop and find a minimal value.

A quick peak at the foor loop?

We use the for loop when we know exaclty how many times our script should run. The syntax is very simple:

for (initialzing counter value; testing condition; incrementing counter) {
  code that needs to be executed for each iteration;
}

Now, let’s look at one simple example and explain the syntax through this:

";
}
?>

The initializing counter value (which initializes the loop counter value) is 1, so the value of the variable is set to start at 1 ($v = 1;).
Next, the example says the testing condition, which is tested or evaluated for each loop iteration is $v <= 15;. In plain English, this translates to continue the loop as long as $v is less than or equal to 15. If the loop iteration does not correlate with the values – if it’s bigger than 15, end the loop.
Incrementing counter is $v += 2, which means that the loop incrementing counter value increases by 2 with each loop iteration.

The result of the code above will be:

The number is: 1
The number is: 3
The number is: 5
The number is: 7
The number is: 9
The number is: 11
The number is: 13
The number is: 15

What if we want to apply two testing conditions in a for loop?

We want to set the conditions in a for loop like so ($v = 1; $v < 15 or $v < $myCounter; $v += 2), or in plain English we want the value of the variable $v to be set to start at 1, and we want the loop to run while $v is less than 15 or less than the value of $myCounter which ever comes first. We want the value of the variable $v to increase with each iteration for 2. For the sake of presentation let’s say $myCounter is 30, so the testing counter is $v < 15 or $v < 30;.
When we run the code we quickly realize the loop gives the following results:


The number is: 1
The number is: 3
The number is: 5
The number is: 7
The number is: 9
The number is: 11
The number is: 13
The number is: 15
The number is: 17
The number is: 19
The number is: 21
The number is: 23
The number is: 25
The number is: 27
The number is: 29

The code works when we set it for each condition on its own, and it also works when we use either OR or AND operator, it just gives different results. We use OR operator to combine two expressions and it returns true if either expression is true. If neither of the expressions is true, it returns false. On the other hand, we use AND operator to combine two boolean expressions. The result is true if both expressions evaluate to be true, if they are not true, it returns false. Both OR and AND operators are short-circuiting.

So, here’s the answer to the question of whether you use two conditions in a for loop in PHP? Yes, you can use two conditions in a for loop in PHP, as a matter of fact, you can use as many conditions in a for loop as you wish.

How can we solve this problem in a different manner?

Since we are searching for a minimum number, we can apply the min() function, because it helps us find the lowest value in an array or the lowest value among several specified values, like so:

min(array_values)

or

min(value1, value2, ...)

In our case, when we (for the sake of easier presentation) said the $myCounter is 30, this function from the perspective of logic doesn’t make much sense, because we can easily recognize which number (15 or 30) is the lowest. Nevertheless, when we have at least two numerical values and we want to find the lowest, the min() function will do the job for us:

“;
}
?>

The result will be in accordance with the logic:

The number is: 1
The number is: 3
The number is: 5
The number is: 7
The number is: 9
The number is: 11
The number is: 13

For a sake of this article, we could also mention max() function, which would help us find the highest value in an array or the highest value among several specified values.