accessing local variables, without passing them?
Posted: Thu Mar 29, 2007 2:48 am
Ok, so what I have is something like this...
The above code doesn't work of course, but I hope it gives you an idea of what I want it to do. I want to be able to access the local variables in numbers() without having to pass it as a function paramater. From add() I can access the global scope, and of course it's own local scope, but what about the scope numbers()? It is being called from there, so I'm hoping there is a way. Is there a way, without making the local variables in numbers() into globals or passing them as parameters?
EDIT: or alternativly, is there a way to pass them all in a single array without having to type them all out? What I don't want to have to do is this:
Code: Select all
<?php
function numbers()
{
$a = 1;
$b = 1;
$c = 1;
$d = 1;
$e = 1;
$f = 1;
$sum = 0;
add();
echo $sum;
}
function add()
{
global $a, $b, $c, $d, $e, $f, $sum;
$sum = $a + $b + $c + $d + $e + $f;
}
numbers();
?>EDIT: or alternativly, is there a way to pass them all in a single array without having to type them all out? What I don't want to have to do is this:
Code: Select all
<?php
function numbers()
{
$a = 1;
$b = 1;
$c = 1;
$d = 1;
$e = 1;
$f = 1;
$sum = 0;
add(array('a' => &$a, 'b' => &$b, 'c' => &$c, 'd' => &$d, 'e' => &$e, 'f' => &$f, 'sum' => &$sum));
echo $sum;
}
function add($locals)
{
$locals['sum'] = $locals['a'] + $locals['b'] + $locals['c'] + $locals['d'] + $locals['e'] + $locals['f'];
}
numbers();
?>