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
Dave2000
Forum Contributor
Posts: 126 Joined: Wed Jun 21, 2006 1:48 pm
Post
by Dave2000 » Thu Nov 09, 2006 6:28 pm
how might i get something similar to this to work...
Code: Select all
$variable = 'attack';
$new_gold = $old_gold + cost_$variable_gold(2);
I want to call a function depending on the value of $variable
How can i get this to work. It is just giving me the error...
Parse error: syntax error, unexpected T_VARIABLE in
Thank you
Shears
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Thu Nov 09, 2006 7:27 pm
Not certain about this, but it can't hurt to try it.
Code: Select all
<?php
$variable = 'attack';
$function = cost' . $variable . 'gold';
$new_gold = $old_gold + $function(2);
?>
Cameri
Forum Commoner
Posts: 87 Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic
Post
by Cameri » Thu Nov 09, 2006 8:55 pm
Everah wrote: Not certain about this, but it can't hurt to try it.
Code: Select all
<?php
$variable = 'attack';
$function = cost' . $variable . 'gold';
$new_gold = $old_gold + $function(2);
?>
Code: Select all
<?php
$variable = 'attack';
$function = 'cost_' . $variable . '_gold';
$new_gold = $old_gold + $function(2);
?>
wyrmmage
Forum Commoner
Posts: 56 Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID
Post
by wyrmmage » Thu Nov 09, 2006 9:25 pm
yep, that's pretty much how you do it...you could always google 'variable composing' to see examples of this using variables also.
-wyrmmage
Dave2000
Forum Contributor
Posts: 126 Joined: Wed Jun 21, 2006 1:48 pm
Post
by Dave2000 » Fri Nov 10, 2006 3:54 pm
Thank you. Problem solved
bokehman
Forum Regular
Posts: 509 Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)
Post
by bokehman » Fri Nov 10, 2006 4:04 pm
You'd need to eval() it.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Nov 10, 2006 4:07 pm
bokehman wrote: You'd need to eval() it.
Most often no, you don't. Unless you're talking about something else or I missed something in the thread.
bokehman
Forum Regular
Posts: 509 Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)
Post
by bokehman » Fri Nov 10, 2006 4:35 pm
feyd wrote: Unless you're talking about something else or I missed something in the thread.
It's quite possible I don't understand the question. I'm looking at this:
Code: Select all
$new_gold = $old_gold.'cost_'.$variable_gold.'(2);';To get that to fire it would need to be evaled, wouldn't it? Anyway that's what I meant.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Nov 10, 2006 4:48 pm
bokehman wrote: Code: Select all
$new_gold = $old_gold.'cost_'.$variable_gold.'(2);';To get that to fire it would need to be evaled, wouldn't it? Anyway that's what I meant.
The way it is written in your snippet, yes it would need eval() however it could easily be adjusted to not need it at all.
Code: Select all
$func = $old_gold.'cost_'.$variable_gold; $new_gold = $func(2);
bokehman
Forum Regular
Posts: 509 Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)
Post
by bokehman » Sat Nov 11, 2006 5:35 am
Code: Select all
<?php
function foo($in)
{
return $in*$in;
}
$func = 'foo';
echo $func(2);
?>Thanks for clarifying that; I didn't know that construction existed.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Sat Nov 11, 2006 7:57 am
Me either, until a couple of weeks ago. I remember asking the poster 'What are you trying to do with that bit of code?' Volka then showed me the light.
wyrmmage
Forum Commoner
Posts: 56 Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID
Post
by wyrmmage » Sat Nov 11, 2006 11:01 am
wow...that is quite interesting code; thanks for posting it