By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,406 Members | 1,504 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,406 IT Pros & Developers. It's quick & easy.

Variable variables in function

P: n/a
Everything works fine if I don't use this as a function and just place it
inside my code. When I use it as a function it will return the variables
that are in the "else" portion, but not the if statement. I can see the
variables and echo them inside the function, but not outside of it, but just
on the "if" portion, the "else" part is working fine.

I must be missing something here, any help would be appreciated!

function SetSessionVars ($postresults)
{
foreach ($postresults as $key => $val)
{
if (is_array($val))
{
foreach ($val as $subkey => $subval)
{
$prefix = $key . "_";
$newvar = $prefix . $subkey;
$_SESSION['payroll'][$key][$subkey] = $postresults[$key][$subkey];
$$newvar = $_SESSION['payroll'][$key][$subkey];
}
}
else
{
$_SESSION['payroll'][$key] = $postresults[$key];
$$key = $_SESSION['payroll'][$key];
}
}
}
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I was messing around with global and didn't have it in the right place, now
I do and it seems to be working fine. Sorry for the post.
"Eraserhead" <er**********@hotmail.com> wrote in message
news:rKNge.316254$4J5.240736@fe04!news.easynews.co m...
Everything works fine if I don't use this as a function and just place it
inside my code. When I use it as a function it will return the variables
that are in the "else" portion, but not the if statement. I can see the
variables and echo them inside the function, but not outside of it, but
just on the "if" portion, the "else" part is working fine.

I must be missing something here, any help would be appreciated!

function SetSessionVars ($postresults)
{
foreach ($postresults as $key => $val)
{
if (is_array($val))
{
foreach ($val as $subkey => $subval)
{
$prefix = $key . "_";
$newvar = $prefix . $subkey;
$_SESSION['payroll'][$key][$subkey] = $postresults[$key][$subkey];
$$newvar = $_SESSION['payroll'][$key][$subkey];
}
}
else
{
$_SESSION['payroll'][$key] = $postresults[$key];
$$key = $_SESSION['payroll'][$key];
}
}
}

Jul 17 '05 #2

P: n/a
Eraserhead wrote:
function SetSessionVars ($postresults){
foreach ($postresults as $key => $val){
if (is_array($val)){
foreach ($val as $subkey => $subval){
$prefix = $key . "_";
$newvar = $prefix . $subkey;
$_SESSION['payroll'][$key][$subkey] = $postresults[$key][$subkey];
$$newvar = $_SESSION['payroll'][$key][$subkey];
}
}else{
$_SESSION['payroll'][$key] = $postresults[$key];
$$key = $_SESSION['payroll'][$key];
}
}
}


Instead of having:
$$key = $_SESSION['payroll'][$key];

Why not have $ar[$key}=... At the top of the function do:
$ar=array();

And at the end to:
return $ar;

Then if you want to make the variables out of the array keys, you can
always call extract() - otherwise, you have a nice array to play with in
case you want to do something else with it... Something like that may
even help in debugging so it is more obvious where variables are coming
from. (Not that I'd play with variables like this unless it was
/absolutely/ *unavoidable*...)

--
Justin Koivisto - ju****@koivi.com
http://koivi.com
Jul 17 '05 #3

P: n/a
Justin Koivisto wrote:
Eraserhead wrote:

function SetSessionVars ($postresults){
foreach ($postresults as $key => $val){
if (is_array($val)){
foreach ($val as $subkey => $subval){
$prefix = $key . "_";
$newvar = $prefix . $subkey;
$_SESSION['payroll'][$key][$subkey] = $postresults[$key][$subkey];
$$newvar = $_SESSION['payroll'][$key][$subkey];
}
}else{
$_SESSION['payroll'][$key] = $postresults[$key];
$$key = $_SESSION['payroll'][$key];
}
}
}

Instead of having:
$$key = $_SESSION['payroll'][$key];


....and $$newvar = $_SESSION['payroll'][$key][$subkey];

(sorry hit post too quickly)

--
Justin Koivisto - ju****@koivi.com
http://koivi.com
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.