is there a better, simpler way? - php

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
}
}

Related

Simplify PHP Condition

I have a loop that creates hexagons 3 in a row, but there is a small bug in the code if 3 are not used per row. To get around this, I have been applying a negative position using conditional code, but it's obviously not going to work on going without adding conditions to each step.
Does anyone know of a way of simplifying the following for future proofing? The condition just checks in increments of 3.
if($i == 3) {
echo ".hex-2 { left: -24.7%;}";
} else if($i == 6) {
echo ".hex-5 { left: -24.7%;}";
} else if($i == 9) {
echo ".hex-8 { left: -24.7%;}";
}
I'd like to do something for this array too, which counts in 2 then 1, then 2 and so on;
if(in_array($i, array(1,3,4,6,7,9,10,12,13,15,16,18,19,21,22,24,25,27,28,30,31))) { echo "</div><div class='hex-wrap'>"; };
Is this possible?
This should do it, though it definitely can be beautified somehow:
$numbers = [];
$n = 1;
$numbers[] = $n;
while ($n < 29) // Or whichever is the allowed maximum number
{
$n += 2;
$numbers[] = $n;
$n += 1;
$numbers[] = $n;
}
And then:
if (in_array($i, numbers)) { echo "</div><div class='hex-wrap'>"; };

PHP Loop Per Tens

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;
}
}

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.

Loop Code Optimization

How can I optimize the following code ,
I Need to run 3 sets of loops like this:
for($i=1;$i<=$count-1;$i++){
for($j=$i+1;$j<=$count;$j++){
// do some query use $i and $j
}
}
for($i=1;$i<=$count-2;$i++){
for($j=$i+1;$j<=$count-1;$j++){
for($k=$j+1;$k<=$count;$k++){
// do some query use $i and $j and $k
}
}
}
for($i=1;$i<=$count-3;$i++){
for($j=$i+1;$j<=$count-2;$j++){
for($k=$j+1;$k<=$count-1;$k++){
for($l=$k+1;$l<=$count;$l++){
// do some query use $i and $j and $k and $l
}
}
}
}
Is there a way to simplify the code, perhaps to connect the loops together ?
thanks !
This should do it (untested):
for($i = 1; $i <= $count - 3; $i++) {
for($j = $i + 1; $j <= $count; $j++) {
// i,j query
if($j > $count - 2) {
continue;
}
for($k = $j + 1; $k <= $count; $k++) {
// i,j,k query
if($k > $count - 1) {
continue;
}
for($l = $k + 1; $l <= $count; $l++) {
// i,j,k,l query
}
}
}
}
Note that the queries are no longer in their original order.
As it has been said, there's no way to optimize this further without knowing the queries you are running.
The big problem is that the inner loops are run multiple times. You can get around this by checking i === 1 and j === 2 inside the loops and only running the appropriate code if true.
Micro-optimization:
Use
++$i
rather than
$i++
and equivalent for $j++, $k++ and $l++
But what are you doing in these loops: it's entirely possible that your do some query (database?) could be changed to remove the loops completely... and that would be far more effective than any micro-optimisations

Categories