I am trying to calculate specific values of a counter in my foreach loop.
I have this if statement in my code
if ( $i == 21 || $i == 41 || $i == 61 || $i == 81 || $i == 101 )
which are equal to
($i * 20) + 1
Instead of writing all these values (21,41,61,81...) I want to create a formula for my code but I couldn't figure out what the result should be equal to inside my if statement
Use modulus:
if ($i % 20 == 1) { ...
http://php.net/manual/en/language.operators.arithmetic.php
Look for the remainder after dividing by 20 using the % operator (modulus).
if ($i%20 == 1)
{
// do stuff
}
Related
I have for loop from 1 to 6000 and I would like to execute a function every 100 times. I have only such an idea:
for($i=0;$i<=6000;$i++)
{
if($i==100 && $i==200 ... $i==6000) do something;
}
How can I solve this problem differently?
From http://php.net/manual/en/language.operators.arithmetic.php
if ($i % 100 == 0) {
// $i can be fully divided by 100
}
The modulo operator (%) tells you if a number divided by another number has a remainder. If the remainder is 0, you know the first number is a multiple of the second (since it divides evenly).
Just check if i is a multiple of 100:
for($i=0;$i<=6000;$i++)
{
if($i % 100 == 0) {
}
}
I agree with the answers this question has received already. However, you might want to omit the case when $i is 0. So you can check it in your for loop if you are starting from 0.
for($i=0; $i<=6000; $i++)
{
if($i != 0 && $i % 100 == 0){
// do something
}
}
I trying to print simple numbers from 1 to 10 using a for loop like this:
for($i = 0; $i <= 10; $i++){
if($i != 4 || $i != 6){
echo $i."<br/>";
}
}
Output:
0
1
2
3
4
5
6
7
8
9
10
I just want the output from 0 to 10 but the output should not contain the numbers 4 and 6.
It is working fine if I use the && operator but does not work if I use||.
I do not understand why this is not working - I think it should work with ||.
You don't want to print either 4 or 6, so you should be using &&.
The statement if($i != 4 || $i != 6) will trigger whenever $i is not equal to 4, or whenever $i is not equal to 6. Considering 4 is not equal to 6, it will trigger in both cases. It will reach $i = 4, and realise that $i is not equal to 6. This will step into the condition, as you say it only has to hold true for one or the other.
The statement if($i != 4 && $i != 6) implies that $i is not equal to 4 and $i is not equal to 6. Both conditions must hold true at the same time. When $i = 4, $i != 6 will be true, but $i != 4 will be false. Both conditions need to be true, so it will fail. Essentially, this could be rewritten as:
for($i = 0; $i <= 10; $i++){
if($i != 4) {
if($i != 6) {
echo $i."<br/>";
}
}
}
To skip over the numbers 4 and 6 in the loop, you have to use the and condition:
for($i = 0; $i <= 10; $i++){
if($i != 4 && $i != 6){
echo $i."<br/>";
}
}
Hope this helps!
You want &&. It would not work with ||; that means something different.
"x && y" means "only true if x is true and y is also true; otherwise false."
"x || y" means "true if either x is true or y is true; only false if both are false."
The contrapositive (i.e., "opposite") of ($i != 4 && $i != 6) is ($i == 4 || $i == 6).
Mixing in the || without swapping the comparisons as well means, in your case, "true if $i is not 4, or also true if $i is not 6." Since one of those cases must always true, the result is also always true.
<?php for ($i=0; $i < 10 ; $i++) {
if ($i != 4 && $i !=6){
echo $i."<br>";}}
?>
Simple question: what's a more compact way to write a conditional like the following:
if ($i != 8 && $i != 19 && $i != 23 && $i != 43) ...
I tried the following approaches with no luck:
if ($i != 8 || 19 || 23 || 43) ...
if ($i != 8 or 19 or 23 or 43) ...
You could collect all of those numbers into an array
$set = array(8, 19, 23, 43);
And then test the value against the set using the in_array() function
if (in_array($i, $set)) { ... }
For this just make array of numbers like bellow
$num = array(8,19,23,43);
and check in_array like bellow.
<?php
$num = array(8,19,23,43);
if(!in_array($i,$num)){
echo "here";
}
?>
How to do something every 5 (for example) cycles inside foreach?
I'm add $i++ How to check it by step?
Use modulo to determine offset.
$i = 0;
foreach ($array as $a) {
$i++;
if ($i % 5 == 0) {
// your code for every 5th item
}
// your inside loop code
}
Unless you're doing something separately in each iteration, don't.
Use a for loop and increment the counter by 5 each time:
$collectionLength = count($collection);
for($i = 0; $i < $collectionLength; i+=5)
{
// Do something
}
Otherwise, you can use the modulo operator to determine if you're on one of the fifth iterations:
if(($i + 1) % 5 == 0) // assuming i starts at 0
{
// Do something special this time
}
for($i = 0; $i < $items; $i++){
//for every 5th item, assuming i starts at 0 (skip)
if($i % 5 == 0 && $i != 0){
//execute your code
}
}
Using a php like so..
for($i = 0; $i < 30; $i++) ...
I have this html element that is rendered several times. I want to, each time we arrive at the sixth element, it adds a "style:margin-right: 0px;" for example.
My question is:
How can we find always the 6th element ?
Update: So that can mark the 6th element, then the 12th element, then the 18th element then the 24th and, at least, the 30th.
Thanks in advance,
MEM
You can use the modulo operator, %:
for ($i = 0; $i < 30; $i++) {
if ($i % 6 == 5) {
# Add what you want---I don't use PHP much
}
}
The modulo operator, %, divides the left hand side by the right hand side, and then reports the remainder of the result. So, for instance, 15 % 6 == 3, because 15 == 6*2 + 3. In the expression a % b == c, c will range from 0 to b-1. If you had $i % 6 == 0 in the above test, it would style the first element, the seventh element, etc.; this way, it'll style the sixth element, the twelfth element, etc. This is because when you're on the sixth element, $i == 5, and 5 % 6 is of course 6. For more information, check out what Wikipedia has to say about the modulo operation.
Check that the mod of $i and 6 is 0 (means that $i is evenly divisible by 6).
for($i = 0; $i < 30; $i++) {
if($i % 6 == 0) {
// this is a sixth element
}
...
}
If you don't want this to happen on the first iteration ($i == 0), you'll also need to add that check to the if statement:
if($i > 0 && $i % 6 == 0){
}
you can try using modulus (%)
if(!($i % 6)) {
// add style
}
or
if(($i % 6) == 0) {
// add style
}
EDIT: Kaleb beats me to it =/