Hello. I would need some help.
I wrote a function that assign value to global variables. The variable can be an array, in this case will add the value to the array.
The functon have 2 parameters
1 - the name of the variable,
2 - the value
For the array is not working. What's wrong?
Here is the code
<?
//p is array
$p[0] = 0;
$p[] = 1;
$common_var = '';
function set($VAR,$VALUE)
{
/
in $SET_VARIABLE i will compute the name of the variable without "[]" if is the case
this is needed in order to use the global statement.
/
$SET_VARIABLE = $VAR;
if (substr($SET_VARIABLE,-2,2) == '[]')
{
$SET_VARIABLE = substr($VAR,0,-2);
}
global $$SET_VARIABLE;
if (isset($$SET_VARIABLE))
{
$$VAR = $VALUE;
}
else {
echo 'Warning: variable '. $SET_VARIABLE .' is unknown';
}
}
// test the set function
set('common_var','100');
echo 'common_var = '. $common_var .';<br>';
// the assignation of the var "common_var" is ok
set('p[]','2');
for ($i=0 ; $i < count($p) ; $i++)
{
echo 'p['. $i .']='. $p[$i] .';<br>';
}
// for the array is not working. why?
?>