Are PHP variables declared inside a foreach loop destroyed and re-created at each iteration?

前端 未结 4 1653
伪装坚强ぢ
伪装坚强ぢ 2020-12-16 15:12

If I declare a variable inside a foreach loop, such as:

foreach($myArray as $myData) {
    $myVariable = \'x\';
}

Does PHP destroy it, and

相关标签:
4条回答
  • 2020-12-16 15:50

    According to the debugger in my IDE (NuSphere PHPed) in your first example:

    foreach($myArray as $myData) {
        $myVariable = 'x';
    }
    

    $myVariable is only created once.

    0 讨论(0)
  • 2020-12-16 15:52

    The problem is $myVariable is not truly local to foreach only. So it can clobber a global variable under the same name.

    A way around that is make your foreach an inline anonymous function.

    E.g.

    $myforeach=function(&$myArray){ // pass by ref only if modifying it
      foreach($myArray as $myData) {
        $myVariable = 'x';
      }
    };
    $myforeach($myArray);  // execute anonymous.
    

    This way you guarantee it will not step on other globals.

    0 讨论(0)
  • 2020-12-16 15:53

    In your first example:

    foreach($myArray as $myData) {
        $myVariable = 'x';
    }
    

    $myVariable is created during the first iteration and than overwritten on each further iteration. It will not be destroyed at any time before leaving the scope of your script, function, method, ...

    In your second example:

    $myVariable;
    foreach($myArray as $myData) {
        $myVariable = 'x';
    }
    

    $myVariable is created before any iteration and set to null. During each iteration if will be overwritten. It will not be destroyed at any time before leaving the scope of your script, function, method, ...

    Update

    I missed to mention the main difference. If $myArray is empty (count($myArray) === 0) $myVariable will not be created in your first example, but in your second it will with a value of null.

    0 讨论(0)
  • 2020-12-16 16:07

    According to my experiment, it's the same:

    <?php
    for($i = 0; $i < 3; $i++) {
        $myVariable = $i;
    }
    var_dump($myVariable);
    

    prints: int(2)

    <?php
    $myVariable;
    for($i = 0; $i < 3; $i++) {
        $myVariable = $i;
    }
    var_dump($myVariable);
    

    prints: int(2)

    0 讨论(0)
提交回复
热议问题