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?

6
  • 5
    What do you think +$i does? Commented May 9, 2017 at 18:14
  • 4
    on top of what John says, $i=1 youre not comparing (==), you're setting. Commented May 9, 2017 at 18:14
  • Dynamic loop by sizeof($kentat_taulukkona), but a break at the 6th iteration. Strange... Commented May 9, 2017 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 Commented May 9, 2017 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. Commented May 9, 2017 at 18:46

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

4 Comments

And also do what Sterling suggested with ++$i
@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)
@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..
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.
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) {...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.