I'm new in PHP, I don't understand why the final result of the code below is '233' instead of '231', isn't the $a in foreach is a temp variable?
<?php
$a = '1';
$c = array('2', '3');
foreach($c as $a){
echo $a ;
}
echo $a;
?>
Can anyone help? Thks.
Updated 2014-11-28
Now I know what was my problem. As the accepted answer and this answer have pointed out, neither the foreach
nor the while
act like functions
, they are just normal sentences just like $a='3';
. So now I know this is my misunderstanding and it's not just about php
, as I've tried in python, it's the same.
a = 123
b = [1, 2, 3]
for a in b:
print a
print a