1

I have this piece of code:

$exists = false;
foreach ($outsiderTests as $key => $outsiderTest) {
   $textExists = null;
   foreach ($tests as $test) {
       if ($outsiderTest->getName() == $test->getName()) {
           $exists = true;
           $existingTest = $test;
           break;
       } else
           $exists = false;
   }

   var_dump($existingTest, $test);
}

As you can see, I want to see if there is an equivalent to an outsiderTest in $tests array. I thought I would have to save the existing equivalent $test on another variable as it would be gone after the foreach ends, but it does not.

The value of $existingTest and $test is the same when I dump them. This is cool, and makes me able to get rid of the mentioned $existingTest variable, but makes me wonder if I am understanding PHP's loop functionality.

Doesn't the $test variable only exist inside the foreach scope? Does PHP temporarily save the value of the last index the execution has run through?

S. Dre
  • 647
  • 1
  • 4
  • 18
  • 1
    There _is_ no "foreach scope" in PHP. – CBroe Apr 04 '22 at 12:08
  • You're both saying ($tests as $key => $test) and ($tests as $test). So in this case, the $test variable you're seeing is the $key from your first foreach (Which you are still inside of when var_dumping.) – Stoff Apr 04 '22 at 12:09
  • Sorry @Stoff, my bad, the code I uploaded wasn't correct. Updated. – S. Dre Apr 04 '22 at 12:10
  • Just an idea, but why not creating an array of `$testNames` before the external foreach loop and then using `if (in_array($outsiderTest->getName(), $testNames)) { ... }` instead of the inner foreach loop? It might be faster than looping and calling `$test->getName()` several times. – Patrick Janser Apr 04 '22 at 12:19
  • @PatrickJanser thanks for the input! The code above is an example, not really a code I am using right now, maybe in that particular piece of code it would be better, but I just wanted to show this particular situation. Although maybe this applies to any situation, but if you are working with objects probably you would have to get the object with that name, in the example above you already have it at your disposal, isn't it? – S. Dre Apr 04 '22 at 12:21
  • 1
    @S.Dre Yes, sorry, I see what you ment with your question. I think that in PHP there's only 2 scopes : the global scope and the local function scope. So a loop variable is visible after the loop scope and it effectively contains the last value. When using a callback function you have to either declare globals in the function or pass them with the [`use ()` syntax if it's an anonymous function](https://www.php.net/manual/en/functions.anonymous.php#example-182). – Patrick Janser Apr 04 '22 at 12:29
  • @PatrickJanser that actually solves my doubt plain and simple. Post it as an answer with that callback function addon (I find it helpful) and you will have it accepted :) – S. Dre Apr 04 '22 at 12:35

1 Answers1

2

PHP's variable scope is explained here: https://www.php.net/manual/en/language.variables.scope.php

Effectively you have 2 scopes:

  • The global scope
  • The local function scope

So a loop variable will be accessible out of it's scope and will contain the last value it had. This is why you got this behaviour.

If you have a loop calling a function then you have multiple options:

  • Declare the external variable with the global keyword inside the function.
  • Access globals with the $GLOBALS variable.
  • Pass the globals you need to your anonymous function with the use () syntax.
Patrick Janser
  • 3,318
  • 1
  • 16
  • 18