0

I want to echo 4 lines of code. The variable $x shall be 1,2,3,4 and the variable $y shall be 3,4,5,6.

The $x variable in my loop works fine, the $y however doesn't work like I want it (so it echoes 3,4,5,6 later in HTML)

So my question is: Why is the $y variable not returning 3,4,5,6 in my final HTML code.

for ($x=0; $x<=3;$x++) {
$y=3;
if (${"interferer" .$x} == true) {
  echo "<li><a href='#tabs-$y'>Interferer $x</a></li>";
}
else {
  echo "<!--<li><a href='#tabs-$y'>Interferer $x</a></li>-->";

}
$y++;
}

5 Answers 5

1

you're setting $y=3; every time, it will never increment.

Try moving $y=3; outside the for loop.

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

3 Comments

We all make these mistakes. Glad to help.
sure, i was waiting for the 10min to pass ;)
Wow is it 10 mins... I thought it was less than that. Perfect, thank you!
1

In every iteration you are making $y=3; and increasing it at the end

Comments

1

Y variable is getting initialize at the start of loop everytime.

Comments

1

You need to update your code so that initialize $y before you enter the for loop

$y=3; //Move outside of for loop
for ($x=0; $x<=3;$x++) {
    if (${"interferer" .$x} == true) {
        echo "<li><a href='#tabs-$y'>Interferer $x</a></li>";
    } else {
        echo "<!--<li><a href='#tabs-$y'>Interferer $x</a></li>-->";

    }
    $y++;
}

How you had it previously mean that $y was getting reset to the value 3 on every iteration of the loop thus destroying the incrementation

Comments

0

it is simply because your $y is reset every loop to three, you need to take it out of the loop....

$y=3;  // here :)
for ($x=0; $x<=3;$x++) {
    //$y=3; //not here!
    if (${"interferer" .$x} == true) {
        echo "<li><a href='#tabs-$y'>Interferer $x</a></li>";
    }
    else {
        echo "<!--<li><a href='#tabs-$y'>Interferer $x</a></li>-->";
    }
    $y++;
}

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.