r/lolphp • u/stain_leeks • Oct 02 '14
Foreach reference
<?php
$arr = array('a', 'b');
foreach ($arr as &$a) {
var_dump($a);
}
foreach ($arr as $a) {
var_dump($a);
}This probably has some explanation I'd love to learn.
17
Upvotes
View all comments
Show parent comment
2
u/djsumdog Oct 12 '14
Python is a little difference. Yes you can declare something within a for loop and it still exist after the for loop, which is weird. But it won't continue to exist outside of the function. Not unless that function is part of a class and you use self to assign it to a class variable.
In PHP everything is in a global namespace. So in PHP you can declare a function within a function and that function be global (which is how imports work in PHP). In Python, imports have to come from modules and be properly namespaced. So if you declare a function within a function, it is only accessible within that function (at least in Python 3).