PHP Loop Per Tens - php

How to make loop per tens like this one,
<?php
for ($i=0; $i < 30; $i++) {
if ($i == 1 || $ == 2 ... $i == 10) {
# code...
} elseif ($i == 11 || $ == 12 ... $i == 20) {
# code...
} else {
# code...
}
}
I need 1 2 3 4 5 is different from 11 12 13 14 15 and 21 22 23 24 25

There are several ways you could approach this :
1. Separate them
Since clearly, you're doing different code to the ranges 1-10, 11-20, a,d 21-30, then it would do you good to separate them into several for loops.
for ($i=1; $i <= 10; $i++) {
//code for $i 1-10
}
for ($i=11; $i <= 20; $i++) {
//code for $i 11-20
}
for ($i=21; $i <= 30; $i++) {
//code for $i 21-30
}
2. Put the conditionals inside one for loop, but use <= instead of ==
for ($i=1; $i <= 30; $i++) {
if($i <= 10){
//code for $i 1-10
}
else if($i <= 20){
// code for $i 11-20
}
else{
// code for $i 21-30
}
}
Alternatively, you could use $i > 0 && $i <= 10 for the if conditions if you prefer or for readability, but the above code does exactly the same with less.
Personally, for your specific example, I would prefer using the first option, as it is much more readable (for me).

How about this:
for($i = 0; $i < 30; $i++) {
switch (floor($i / 10)) {
case 0:
break;
case 1:
break;
default:
break;
}
}

Related

For loop (php) that results in this: 12345678910987654321

My niece is trying to create one for-loop (php), that results in this:
* 12345678910987654321
example for loop she tried:
for ($i = 1; $i <= 10; $i++ , $i = 10; $i <= 1; $i--) {
echo $i . ' ';
}
She can only use if's and elseif's. I'm not a programmer and can't really help her. Any ideas how this could be achieved in php?
Any information would be greatly appreciated.
The key is to add a variable instead of a number, then reverse that number when $i hits 10.
for($i = 1, $j = 1; $i> 0; $i+=$j) // Start i at 1, and j at 1
{
echo $i;
if($i == 10)
$j = -1; // i has hit 10, so use -1 to start subtracting
}
Another possibility is to loop up to 20, printing $i for the ascending part and 20 - $i for the descending.
for ($i = 1; $i < 20; $i++) {
if ($i <= 10) {
echo $i;
} else {
echo 20 - $i;
}
}

How can i make the for loop parameters dynamic

for ($i=40; $i>=30; $i--) //code will display data for top x row
for ($i=1; $i<=9; $i++) //code will display data for left y column
for ($i=29; $i>=21; $i--) //code will display data for bottom x row
for ($i=30; $i>=39; $i++) //code will display data for right y column
These 4 loops all do the same thing.
In my index.php im using "include" to get the 4 loops that are in 4 different files.
How can I make the for loop dynamic?
Algoritihm:
$i = (40,1,29,30) <--will be any of those 4
$maxlow = (30,9,21,39)
$check =(>,<) <--value depends on whether $i > or < $maxlow
$icrement = (--,++) <-- if $check is > then decrease, otherwise increment
for ($i; $i($check)=$maxlow; $i($increment) <---what i am trying to do
// $step is either 1 (incrementing) or -1 (decrementing)
foreach (range($begin, $maxlow, $step) as $i) {
}
Settings for given loop:
$diff = -1;
$start = 40;
$stop = 30 + $diff;
The loop itself, always like this:
for ($i = $start; $i != $stop; $i += $diff)
$low = [40,1,29,30](rand(0,3);
$high = [30,9,21,39](rand(0,3);
$modifier = ($low > $high) ? -1 : 1;
for($i = $low; ($i * $modifier) < ($high * $modifier); $i += $modifier)
{
doStuff();
}
why not just use for with switch inside, like this:
for ($i=1; $i >=39; $i++) {
switch($i) {
case ($i>=1 && $i<=9):
break;
...
...
case ($i>40):
//do something
break;
}
}
It will be much more readable, and easier to understand/edit in future.

optimize the array with for loop

I agree to build a function that will calculate "priceinterval". This means that you build an array that you want to assign values for exampel number between 1-3 to assign "1-3" example $array [2] => "1-3" and 4-10 $array[5] => "4-10"
I've built a function that works but want optimize function, someone who is good at for-loops;)
public function CalcPris() {
$Prisinterval = array();
for ($i = 0; $i <= 3; $i++) {
array_push($Prisintervall, "1-3");
}
if ($i = 4) {
for ($i = 4; $i <= 10; $i++) {
array_push($Prisintervall, "4-10");
}
}
if ($i = 11) {
for ($i = 11; $i <= 50; $i++) {
array_push($Prisintervall, "11-50");
}
}
if ($i = 51) {
for ($i = 51; $i <= 100; $i++) {
array_push($Prisintervall, "51-100");
}
}
var_dump($Prisinterval);
}
the reason for this feature is that you should get a price floor.
in my next step is to check what the number "2" is worth, therefore, Please I build an array that has already been completed as has the value 1-100
your approach is totally off...
you want something like this:
function printerval($int){
if($int >= 1 && $int <= 3){
return "1-3";
}elseif($int >= 4 && $int <= 10){
return "4-10";
}elseif($int >= 11 && $int <= 50){
return "11-50";
}elseif($int >= 51 && $int <= 100){
return "51-100";
}
return "error";
}
calling printerval(2); will return the string "1-3"
well here is the stupid thing that seems to be requested:
function printerval(){
$arr= array();
for($i=1;$i<=100;$i++){
if($i >= 1 && $i <= 3){
$arr[$i]= "1-3";
}elseif($i >= 4 && $i <= 10){
$arr[$i]= "4-10";
}elseif($i >= 11 && $i <= 50){
$arr[$i]= "11-50";
}elseif($i >= 51 && $i <= 100){
$arr[$i]= "51-100";
}
}
return $arr;
}
although that is the wrong approach it should now answer the question..
Ok, to optimize this function, you should do the following:
Go to the nearest technical bookstore, or register at oreilly.com.
Buy a copy of Learning PHP5.
Read the book and understand the difference between if ($x = 4) and if ($x == 4).
Read the book and learn about for-loops and arrays.

Two conditions in a for loop

Why does this for loop work with each condition on their own, but together the first condition doesn't matter?
for ($j = 0; $j < 5 or $j < $synCount; $j++)
I only want the loop run five times
or
if synCount is less than this.
You probably mean "$j under 5 and $j under $sysCount", or:
$j < min(5, $sysCount)
very simple
$j < min(5, $sysCount)
You can youse the break statement to leave the for loop :
for ($j = 0; $j < 5 ; $j++)
{
if( $j >= $synCount )
break;
//treatment
}
Or calculate your limit before the loop :
$ max = $synCount < 5 ? $synCount : 5;
for ($j = 0; $j < $synCount ; $j++)
{
//treatment
}
Another solution,quickest : use min() :
for ($j = 0; $j < min(5, $synCount) ; $j++)
{
//treatment
}
Try it like this:
$loopcount = ($syncount < 5) ? $syncount : 5;
for ($j = 0; $j < $loopcount; ++$j) {
}
The first line determines whether $syncount is less than 5 or not and then assigns a value to $loopcount based on that. Then the loop runs the required number of times.
for ($j = 0; $j < ($syncCount <= 5 ? $syncCount : 5); $j++)
or slightly optimized (but I guess with 5 or less iterations this absolutely doesn't matter)
for ($j = 0, $limit = min($syncCount, 5); $j < $limit; $j++)
Anothe nice solutions
foreach (range(0, min($syncCount, 5)) as $j)
Sidenote
$syncCount <= 5 ? $syncCount : 5 == min($syncCount, 5)
$productsprice=ProductPrice::model()->findAllByAttributes(array ('product_id'=>$products_data->product_id));
foreach($productsprice AS $productsprice):
for($quantity = 0; $quantity <= 10; $quantity++)
{
echo '<li >'.array(value=>'ProductPrice::model()->getquantity($data->quantity)').'</li>' ;
}
endforeach;
Two conditions may indeed be applied inside for loop using logical operator. For example check status until it's ok
for($i = 0; $i < 100 && $status != 'ok'; $i++){
sleep(1);
$status = checkStatus();
}
Otherwise you need to use additional if expression with break
for($i = 0; $i < 100; $i++){
sleep(1);
$status = checkStatus();
if($status == 'ok') break;
}

is there a better, simpler way?

in php, i'm doing a loop, something like like
$x = 0;
for ($i = 0; $i < 100; $i++)
{
if ($x == 3) //better way to do this? in this example, determine every 3 times in a loop
{
//"do something"
$x = 0;
}
$x++;
as you can see, i'm doing something in the loop every 3 times it goes around, but the question is, is there a better, simpler way of finding out if it's time for it to "do something" in the loop?
You could use a modulo:
if(($i % 3 == 0) && ($i > 0))
{
...
}
(don't forget to check if it's superior to 0 or it will get in the if at the first iteration.
You can use the modulus operator % to see if $i is divisible by three. That should keep you from managing $x
Instead of using $x, just check if( $i > 0 && $i % 3 == 0). If you want something to happen on the first run as well, just drop the $i > 0 && part.
Fizzbuzz huh? You'll want the Modulo operator - %
if ((6 % 3) == 0) {
echo "is divisable by three";
}
You can avoid the $x variable by using the modulus operator:
if ($i % 3 == 0 && i > 0){
// Do something
}
Basically this implements the same logic; every third iteration, the loop will run your extra code.
Use modulus on $i:
for ($i = 0; $i < 100; $i++)
{
if($i % 3 == 0)
{
// do something
}
}
for ($i = 0; $i < 100; $i++) {
{
if($i%3==0){
//Do Something
}
}
in this code :
for ($i = 0; $i < 100; $i++)
{
if(($i%3==0) && $i>0)
{
//Do Something #first will be after 4 time
}
}
but this one :
for ($i = 1; $i <=100; $i++)
{
if($i%3==0)
{
//Do Something #even first will be after 3 time
}
}

Categories