473,387 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Variable variables in function

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
3 1240
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
148
by: onkar | last post by:
Given the following code & variable i . int main(int argc,char **argv){ int i; printf("%d\n",i); return 0; } here i is allocated from bss or stack ?
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.