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

前端 未结 4 1676
伪装坚强ぢ
伪装坚强ぢ 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.

提交回复
热议问题