[php] variables

Status
Not open for further replies.

taekwondokid42

New Member
Messages
268
Reaction score
0
Points
0
I want to have a variable with a name that consists of a variable.

My first varible is $war_type.

My second variable is $[$war_type]_cost.


How could I make it so that the engine recognizes $$war_type_cost, but at the same time, recognizes that $war_type is separate from $_cost?

Sorry if this is confusing; I couldn't think of a better way to phrase the problem.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
You need to use curly braces and concat the variable to the string. Like this:

PHP:
$war_type = 'x';
${$war_type . '_cost'} = 10;
//Now you have the variable $x_cost
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
You need to use curly braces and concat the variable to the string. Like this:

PHP:
$war_type = 'x';
${$war_type . '_cost'} = 10;
//Now you have the variable $x_cost

If $war_type is based on user input, make sure it consists of legal characters ([a-zA-Z_]) or you might end up with php errors.
 

cowctcat

New Member
Messages
401
Reaction score
0
Points
0
please close your threads when you are finish otherwise i have to do it
 
Status
Not open for further replies.
Top