This is what's called scope. The $i and $people in your function are local variables. They are not the $i and $people you're using outside of the function. The variables are in different scopes; literally, they are part of two different symbol tables for the interpreter. If you want to modify them, either pass them in by reference, or make the pair a return value.
You can bring the other variables into scope by making them globals, but unnecessary use of global variables leads to messy, hard to maintain code.
PHP Code:
function writeArray($name,$email){
global $i;
global $people;
$i++;
echo $i;
$people[$name] = $email;
print_r($people);
}
versus
PHP Code:
function writeArray($people, $name, $email) {
...
return $people;
}
$people = writeArray($people, $name, $email);
If $i just keeps track of the size of the array, count($people) would give you that directly.
Bookmarks