If you have a simple counter loop, how do you detect special patterns, for example, every 10 increments but at 6/16/26/36. $i needs to start at 0 too.
The only approach I can think of is this one, but obviously it doesn't work for large loops:
for ($i=0; $i < 1000; $i++) {
// if ( $i == 6 || $i == 16 || $i == 26...... etc ) { do something }
}
There's not going to be one single answer for all types of patterns, but so long as there is a pattern, you can figure it out:
for ($i=0; $i<1000; $i++) {
if (($i-6)%10 == 0) {
// every time $i minus 6 is evenly divisible by 10
}
}
Related
I'm somewhat new to PHP, been reading a few books and I've never seen a loop where it gets you all the even numbers(for example from 1 to 10), so I decided to try it myself:
for($i=0;$i<10 && $i % 2===0;$i++)
echo $i;
Tried with only double == as well.
And this,
$i=0;
do echo $i; while($i++<10 && $i % 2 ==0);
Can't seem to figure out how to use 2 conditions in the same statement.
Would appreciate the help!
Thanks.
Try to use this code
for( $i=0; $i<=10; $i++ )
{
if( $i%2 == 0 ){
echo $i;
}
}
The loop is breaking entirely when the second condition fails the first time. On the first iteration: 0 is less than 10, and it is even, so the loop iterates. On the second iteration: 1 is less than 10, but is odd, so the loop breaks.
Your code is the equivalent of this:
for($i=0; $i<10; $i++) {
if ($i % 2 !==0 ) {
break;
}
echo $i;
}
0
You can eliminate the second condition of your for loop to prevent the breakage and rely exclusive on a third expression to increment $i by two each iteration.
for($i=0; $i<10; $i = $i + 2) {
echo $i;
}
02468
The second statement in a for-loop is/are the condition(s) which gets checked every loop. so if it fails your loop stops. what you need will look somewhat like this:
for ($i = 0; $i < 10; $i++)
if ($i % 2 == 0)
echo $i;
So the loop will run over every number but only print out the even ones.
You don't need to loop.
Range can create a range with third parameter step 2.
$arr = range(0,20,2);
Echo implode(" ", $arr);
https://3v4l.org/S3JWV
you can use also regular loop and get the evens by formula:
for($i=0; $i<10 ;$i++) {
$j = $i * 2;
// do somthing with $j witch loop over 10 first evens...
}
This question already has answers here:
PHP: How do you determine every Nth iteration of a loop?
(8 answers)
Closed 3 years ago.
In php i have loop e.g.
for ($i = 0; $i <= 1000; $i++) {
if ($i = 10 || $i == 20 || $i == 30 || $i == 40 || $i == 50 || $i == 60) {
echo $i;
}
}
imagine i need to echo $i every 10,20,30,40,50,60,..970,980,990 there should be way to not write 100 conditions in if statement. Is there some logical way to see if $i increased by 10 then do something like:
if ($i == $i+10) {
...
}
P.S. if possible i dont want to introduce another variable to count i need to find solution with using only $i
Try:
if ($i % 10 == 0)
That will trigger whenever your index is exactly divisible by 10.
rewrite your for loop to:
for ($i = 0; $i <= 1000; $i+=10) {
And I don't know whether it worked for you with commas like this (as in your initial post):
for ($i = 0, $i <= 1000, $i++) {
Skip extra looping:
for ($i = 10; $i <= 1000; $i=$i+10) {
echo $i;
}
Or if you still want to loop every single digit between:
for ($i = 0; $i <= 1000; $i++) {
if( $i % 10 === 0 ) {
echo $i;
}
}
Test Here
Put this inside your main loop. '%', or 'mod', gives you the remainder of $i / 10. If the remainder is '0', then you want to print.
if(0 === ($i % 10)) {
echo $i;
}
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 =/
I did some searching and was not able to find any information regarding this implementation versus every other one I have seen.
function sieve($top)
{
for($i = 11; $i<$top; $i+=2)
{
if($i % 3 == 0 || $i % 5 == 0
|| $i % 7 == 0)
{
continue;
}
echo "$i <br />";
}
}
Yeah I know it just prints it out, but that's not the important part. What is the major pitfall whether it be time or other?
EDIT: Are there any other issues beyond scalability? Also again thanks for the comments about moving forward with prime finding.
The major pitfall of this is it doesn't scale. Once the numbers are large enough anything will be returned. You list of modulus excluders needs to grow with the search.
You can refer to Sieve of Eratosthenes on Wikipedia; and this link for a PHP implementation.
It's limited to prime numbers up to 11. To extend it any further you need to add || $u % 11 == 0 || $i % 13 == 0 ... etc
First, you're only checking against three numbers (3, 7, and 11). For the Sieve of Erathosthenes, you should start with a list of numbers, 2..i. Then loop through that list, and remove numbers that are factors of the number you're iterating on. For example, once you get to 7, which is prime, you'll need to remove 49, 56, and other multiples of 7.
Second, the method I just described would scale very poorly - if you tried looking for primes from 1..10^9, you'd need 10^9 values in your list. There are other ways besides the Sieve of Erathosthenes to find prime numbers - see http://en.wikipedia.org/wiki/Prime_number
This function use "Sieve of Eratosthenes Algorithm"
function getPrimaryNumbers($n)
{
$sieve = [];
for($i = 1; $i <= $n; $i++) {
$sieve[$i] = $i;
}
$i =2;
while($i * $i <= $n) {
if(isset($sieve[$i])) {
$k = $i;
while ($k * $i <= $n) {
unset($sieve[$k * $i]);
$k++;
}
}
$i++;
}
return $sieve;
}