[php] variables

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by taekwondokid42, Apr 13, 2008.

Thread Status:
Not open for further replies.
  1. taekwondokid42

    taekwondokid42 New Member

    Messages:
    268
    Likes Received:
    0
    Trophy 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.
     
  2. woiwky

    woiwky New Member

    Messages:
    390
    Likes Received:
    0
    Trophy 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
     
  3. marshian

    marshian New Member

    Messages:
    526
    Likes Received:
    9
    Trophy Points:
    0
    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.
     
  4. taekwondokid42

    taekwondokid42 New Member

    Messages:
    268
    Likes Received:
    0
    Trophy Points:
    0
    thank you. Reputation to both of you.
     
  5. cowctcat

    cowctcat New Member

    Messages:
    401
    Likes Received:
    0
    Trophy Points:
    0
    please close your threads when you are finish otherwise i have to do it
     
Thread Status:
Not open for further replies.

Share This Page