0

Hi so i just frequently started doing PHP at school and now I have this project in school and when my code gets to this part:

for ($i=0; $i<=sizeof($kentat_taulukkona); +$i) {
    echo $i;
    if($i=1) {
        $a = "=".$arvot_taulukkona[$i];
        $aa = $kentat_taulukkona[$i];
    } else if($i=2) {
        $b = "=".$arvot_taulukkona[$i];
        $ab = ", ".$kentat_taulukkona[$i];
    } else if($i=3) {
        $c = "=".$arvot_taulukkona[$i];
        $ac = ", ".$kentat_taulukkona[$i];
    } else if($i=4) {
        $d = "=".$arvot_taulukkona[$i];
        $ad = ", ".$kentat_taulukkona[$i];
    } else if($i=5) {
        $e = "=".$arvot_taulukkona[$i];
        $ae = ", ".$kentat_taulukkona[$i];
    } else if($i=6) {
        $f = "=".$arvot_taulukkona[$i];
        $af = ", ".$kentat_taulukkona[$i];
        break;
    }
}

it becomes an infinite loop. With that echo i found out it first echoes one 0 and after that number 1 till the end of the world.It might be just a typo what I'm missing or have I understood something wrong since to my knowledge this should work?

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • 5
    What do you think `+$i` does? – John Conde May 09 '17 at 18:14
  • 4
    on top of what John says, `$i=1` youre not comparing (`==`), you're setting. – castis May 09 '17 at 18:14
  • Dynamic loop by sizeof($kentat_taulukkona), but a break at the 6th iteration. Strange... – Sterling Beason May 09 '17 at 18:34
  • Instead of creating all these variable, you should use arrays. To improve your code you can also search about: `foreach`, `range`, and if you are hot: `array_map` – Casimir et Hippolyte May 09 '17 at 18:43
  • Yeah Sterling i did it out of desperation since I couldnt understand whats wrong i tried all kinds of dumb stuff, there is also only one '+' because before i took one off it always repeated number 2 and i thought i had misunderstood it and that with '++' it would always add two. – Tired Chibi May 09 '17 at 18:46
  • I tried to do it with arrays before but since they need to form a mysql sentence(? not too familiar with English sorry) i couldn't get arrays to work. But that might just be me and my limited knowledge. – Tired Chibi May 09 '17 at 18:54

2 Answers2

2

You need to use the comparison operator like == and not the assignment operator =

So your code should be:

if($i==1) {
    $a = "=".$arvot_taulukkona[$i];
    $aa = $kentat_taulukkona[$i];
}else if($i==2) {
    $b = "=".$arvot_taulukkona[$i];
    $ab = ", ".$kentat_taulukkona[$i];
} else if($i==3) {
    $c = "=".$arvot_taulukkona[$i];
    $ac = ", ".$kentat_taulukkona[$i];
} else if($i==4) {
    $d = "=".$arvot_taulukkona[$i];
    $ad = ", ".$kentat_taulukkona[$i];
} else if($i==5) {
    $e = "=".$arvot_taulukkona[$i];
    $ae = ", ".$kentat_taulukkona[$i];
}else if($i==6) {
    $f = "=".$arvot_taulukkona[$i];
    $af = ", ".$kentat_taulukkona[$i];
    break;

Also if you want to keep increasing value of $i by use $i++ thus making your code look like:

for ($i=0; $i<=sizeof($kentat_taulukkona); $i++) {
    echo $i;
    if($i==1) {
        $a = "=".$arvot_taulukkona[$i];
        $aa = $kentat_taulukkona[$i];
    } else if($i==2) {
        $b = "=".$arvot_taulukkona[$i];
        $ab = ", ".$kentat_taulukkona[$i];
    } else if($i==3) {
        $c = "=".$arvot_taulukkona[$i];
        $ac = ", ".$kentat_taulukkona[$i];
    } else if($i==4) {
        $d = "=".$arvot_taulukkona[$i];
        $ad = ", ".$kentat_taulukkona[$i];
    } else if($i==5) {
        $e = "=".$arvot_taulukkona[$i];
        $ae = ", ".$kentat_taulukkona[$i];
    } else if($i==6) {
        $f = "=".$arvot_taulukkona[$i];
        $af = ", ".$kentat_taulukkona[$i];
        break;
    }
}

UPDATE as suggested by @SterlingBeason plase consider using the prefix ++$i or the postfix $i++ operator according to your needs. To read more about the operators in detail check the answer at SO What's the difference between ++$i and $i++ in PHP?

Community
  • 1
  • 1
Shakti Phartiyal
  • 6,156
  • 3
  • 25
  • 46
  • And also do what Sterling suggested with `++$i` – Jeremy Harris May 09 '17 at 18:17
  • @ShaktiPhartiyal prepending and appending the "++" to an int variable do different things. The ++ need to prepend the $i variable for the first IF condition to be met as true. ($i==1) – Sterling Beason May 09 '17 at 18:20
  • @SterlingBeason I agree with you the operation of prefix and postfix operatiors is different. Not sure whether the OP wants to use which one.. But surely will add it in my answer.. – Shakti Phartiyal May 09 '17 at 18:22
  • Thank you for your answer, i can't believe i did that, and on top of it didn't notice it. I will keep it as ++$i since I'm not too sure if understand the difference, and it seems to work either way. Thank you. – Tired Chibi May 09 '17 at 18:48
0

Change the first line of the loop to this:

for ($i=0; $i<=sizeof($kentat_taulukkona); ++$i) { ....

And your IF statements needs to be changed to this (double equal sign to test equality):

if($i==1) {...
Sterling Beason
  • 622
  • 6
  • 12