Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP variable scope issue

Hi guys, I just typed out a huge message, backspaced and lost it, so to save frustration I'll keep this brief and will miss out any unnecessary syntax etc.

I have this kind of set up:

counter1 = 0;
counter2 = 0;
ForEach {
    echo counter1;
    counter1 +=1;
        ForEach {
            echo counter1;     <----- A
            echo counter2;    
            counter2 +=1;
            IF statement {
                echo counter1 . "-" . counter2;  <------- B
            }
        }
    }

The problem is that although at "A" counter1 increments as imagined, counter1 within the IF statement, at point "B", does not and stays at 1.

It seems that the IF statement picks up the counter1 value from the global scope. Is it possible to target counter value that is set within the first ForEach absolutely?

I'm a bit stuck and have tried all that I can think of :/

Hope this makes sense!

3 Answers

I'm not sure I'm understanding your question/code correctly. Please post your full php code you are trying. You are only incrementing $counter1 once, so at point B it should stay at 1 because you aren't incrementing it again.

You said:

It seems that the IF statement picks up the counter1 value from the global scope.

Yes. That's the scope of the counter1 variable and therefore why it's using it (the variable) and it's value (1).

The thing is, $counter1 increments within the nested ForEach as required, so 1,2,3 etc. but when $counter1 is echoed from inside the IF statement it doesn't. I would have thought that the IF would share the same values as the nested ForEach.

Sorry for the lack of clarity - I'm not on good form today, thanks to partying a bit too much last night -__-

I'll clean up my code and post what I have, at the moment its riddled with echoes showing variable values etc. for test purposes.

Again, I'm not 100% sure what you mean, if you clean up your code then that will help, but a variable will not increment, unless you tell it to do so.

Ok, So I took a bit of a break from this problem but have tried tackling it again tonight...

Here's some of my code that should illustrate the problem:

foreach ($blocks as $block) {

                    $blockHTML = "";

                    $blockCounter +=1;

                    $blockHTML = $blockHTML . '<div class="bigleft" id="p' . $blockCounter . '">';

                    echo " _" . $blockCounter . "_ ";


                                foreach ($products as $product_id => $product) { 

                                        $position += 1;

                                        $positionCheck = $totalProds - $position;



                                        if ($positionCheck >= $productsFloor && $positionCheck < $productsCeil) {

                                        $IDposition += 1;

                                        echo "--" . $blockCounter . "-- ";

                                        $previewHTML = $previewHTML . getPreviewHTML($product_id,$product,$blockCounter);


                                        }
                                    }

Obviously from this the only output will be the two "echos"... which together, as the ForEach's loop render as so:

1 --1-- --1-- --1-- --1-- --1-- --1-- 2 3

(the parent ForEach loops 3 times and the child ForEach loops 6 times)

Again, I'm confused. What do you want the output to be? What do you think the output should be?