0

I couldn't understand why for loop gives two answers for same queries.

For for Loop as follows,

for($i=1; $i<=$packcount; $i++)
            {
                $user->trees()->create([
                    'tin' => $settings->settings_value++, // Starts at 1000
                    'validity' => 1,
                ]);

                $settings->settings_value = $settings->settings_value++;
                $settings->save();
            }

// $packcount = 3

While executing, $b gives us 1002 and $d gives as 1003. Why is this ?

Please do let me know if am doing anything wrong.

Arvindh
  • 600
  • 1
  • 10
  • 25
  • You are incrementing the same object. Otherwise, you do a `clone`. I'm sure there is a duplicate page here that does an eloquent job of explaining this. Volunteers, please help me to find it. – mickmackusa Jan 14 '20 at 05:03
  • When you assign `$settings->settings_value` to each variable you are just passing it as a reference, hence when you increment `$d` the `$b` also gets incremented. – Presmelito Jan 14 '20 at 05:08
  • I refactored, still i get the same output – Arvindh Jan 14 '20 at 05:14
  • Can you please check now @Presmelito – Arvindh Jan 14 '20 at 05:18

0 Answers0