Variable Scope in PHP
Many Times Globality of variables will be the small issue, after long time I decided to use super globals. Super globals exists any where: $_SERVER, $_GET, $_POST ..... Now for example:
Note: the key must not be passed by the page via _POST method by the form, else the value will be over written
<?php
$foo[] = range(0, 3);
$_POST['foo'] = $foo;
a(); //no parameters needed.
b();
$foo = $_POST['foo'];
Print_r($foo);
/* out
Array
(
[0] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
[2] => 6
[3] => 7
)
[2] => Array
(
[0] => 8
[1] => 9
[2] => 10
)
)
*/
function a(){
$_POST['foo'][] = range(4, 7);
}
function b(){
$_POST['foo'][] = range(8, 10);
}
?>
PHP Manual