Problem passing variables to functions.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tpotman777
Forum Newbie
Posts: 9
Joined: Wed Nov 11, 2009 5:06 am

Problem passing variables to functions.

Post by tpotman777 »

Hello.

Probably pretty easy stuff, but can't figure it out:

I have a string list of variables, for instance:
$vars = '$a1,$a2,$a3';

which I would like to pass to list() -function:
list($vars) = $db->sql_fetchrow($result)

Of cours now $vars gets the value from the query, but what I would like list to have is:
list($a1,$a2,$a3) = $db->sql_fetchrow($result)

Any idea how I could output the contents of $vars to the list() -function?

Thanks,
Tom
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problem passing variables to functions.

Post by jackpf »

I think the only way to do this is with eval().
tpotman777
Forum Newbie
Posts: 9
Joined: Wed Nov 11, 2009 5:06 am

Re: Problem passing variables to functions.

Post by tpotman777 »

jackpf wrote:I think the only way to do this is with eval().
Umm, how could I implement that in my code?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problem passing variables to functions.

Post by jackpf »

Something like:

Code: Select all

$vars = '$a1,$a2,$a3';
 
eval('list('.$vars.') = $db->sql_fetchrow($result);');
I think.
tpotman777
Forum Newbie
Posts: 9
Joined: Wed Nov 11, 2009 5:06 am

Re: Problem passing variables to functions.

Post by tpotman777 »

Ok, looks reasonable! I actually also have 'while' -statement in there, but just can't get the syntax right:

eval('while list('.$vars.') = $db->sql_fetchrow($result){ echo "something"; }');

gives: Parse error: syntax error, unexpected T_LIST, expecting '('

Any clues on that?

Thanks!
Tom

jackpf wrote:Something like:

Code: Select all

$vars = '$a1,$a2,$a3';
 
eval('list('.$vars.') = $db->sql_fetchrow($result);');
I think.
tpotman777
Forum Newbie
Posts: 9
Joined: Wed Nov 11, 2009 5:06 am

Re: Problem passing variables to functions.

Post by tpotman777 »

Ok, got it working, thanks for your help!

-T
tpotman777 wrote:Ok, looks reasonable! I actually also have 'while' -statement in there, but just can't get the syntax right:

eval('while list('.$vars.') = $db->sql_fetchrow($result){ echo "something"; }');

gives: Parse error: syntax error, unexpected T_LIST, expecting '('

Any clues on that?

Thanks!
Tom

jackpf wrote:Something like:

Code: Select all

$vars = '$a1,$a2,$a3';
 
eval('list('.$vars.') = $db->sql_fetchrow($result);');
I think.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problem passing variables to functions.

Post by jackpf »

Cool.
Post Reply