if $count is a multiple of 3 + 1 in php - php

In php i want to calculate if my count in a foreach loop is a multiple of 3 + 1.
For example if it is 4,7,10,13,16 you get the point.
if( $count % 3 != 0)
This is the closest ive come to finding an answer

What about this?
if ($count % 3 == 1)

This should work:
if ( ($count - 1) % 3 == 0)

Try maybe this:
if ( $count % 3 == 1)

Related

calculate specific values of counter inside foreach loop

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
}

Every 3rd Iteration of PHP Loop

When I say 'Every 3rd Iteration' I mean 'Every 3rd Iteration... starting from number 4'
If it were just every 3rd, I would target the appropriate iterations like so:
<?php if ($count % 3 == 0) : ?>
Bear in mind I've set $count to 1 beforehand
But the iterations need to start with the 4th, then 7th, 10th etc.
Does anyone know how I can acheive this? Can I do it with a simple if statement like the one above?
Unfortunately I don't think a for loop will be possible as this is a WordPress loop I'm dealing with
Your thoughts about using the modulus operator are sound, you just need to expand the scope of your logic surrounding it:
You want every third iteration after the initial 4 have passed. Begin your logic after the first 4:
if ($count > 4)
Then, you want each 3 after that. Your counter includes the initial 4 iterations you didn't want to include, so remove that from your counter and check if the current iteration is a multiple of 3:
if (($count - 4) % 3 === 0)
This should give you what you're looking for.
if ($count > 4)
{
if (($count - 4) % 3 === 0)
{
...
}
}
You can also put this into one line:
if ($count > 4 && ($count - 4) % 3 === 0)
{
...
}
foreach($array as $key=>$value){
if($key % 3 != 0){
continue;
}
}

PHP + Echo Data Routinely

I have the following PHP code working 'successfully' to display URL's:
<?php
for ($i = 0; $i <= $json->domaincount; $i++) {
echo '<td class="domainList">'.$json->$i.'</td>';
}
?>
Every forth echo I want to also
echo </tr><tr>
to start a new line in the table.
Is there an easy way to know which is every forth count?
I have $i which increments from 0 up so when it gets to 3, 7, 11 etc I need to change table line.
thx
Try this:
if ( ( $i + 1 ) % 4 == 0 ) { echo '</tr><tr>'; }
This is using the modulus operator. It divides a number and returns the remainder, so 7 % 4 = 3 (because 4 fits in once, and three is left over) and 8 % 4 = 0 (because 4 fits in evenly and there are no left overs)
Look into modulo operands (% in PHP). So the check that i % 4 == 0 would give you every fourth row.
<?php
for ($i = 0; $i <= $json->domaincount;) {
echo '<td class="domainList">'.$json->$i.'</td>';
if (!(++$i & 3))
echo '</tr><tr>';
}
?>
!($i & 3) is just a quick way of writing $i % 4 === 0 without having to use modulus. You can only use that trick for modding by powers of 2.

Why is my conditional echo-ing the wrong thing

I have something like
just a snipplet
$i = 1; while (...) {
echo ($i % 5 == 1) ? 'class="first-col"' : ($i % 5 == 0) ? 'class="last-col"' : '';
$i++;
}
but even when $i % 5 == 1, I will get class="last-col" echo-ed is my logic right?
This is actually a CSS fix for IE so that I wont need to use nth-child. I am trying to target the 1st and last columns of my grid which contains 5 col/row
The ?: operator is left-associative, i.e. you have
echo ( ($i % 5 == 1) ? 'class="first-col"' : ($i % 5 == 0) ) ? 'class="last-col"' : '';
See http://php.net/manual/en/language.operators.comparison.php
It is best not to nest ternary operators.
Better use if / elseif / else constructs, they are more legible.
adding parenthesis helps:
echo (($i % 5 == 1) ? 'class="first-col"' : (($i % 5 == 0) ? 'class="last-col"' : ''));
For a start you're using nested ternary operations. I'd at least use brackets around the individual conditions to make it obvious what should be carried out first.

Find multiples of a number in PHP

I want to find all muliples of a number in PHP.
I'm using something like this
if($count != 20 )
to work out if $count is not equal to 20.
But I also need this script to check if $count is not equal to 20, 40, 60, 80, 100, 120, 140, 160 etc.
Any ideas? I think i need to use the modulus symbol (%), but I don't know.
if ($count % 20 != 0)
if ($count % 20 != 0)
{
// $count is not a multiple of 20
}
If you don't want zero to be excluded:
if ($count % 20 != 0 || $count == 0)
You can do it like so:
if($count % 20 != 0)

Categories