Strings and Concatination

Discussion in 'PHP' started by timallard, Aug 13, 2007.

  1. #1
    Hello im trying to do this:

    Im deep into strings right now and i have a line where i need to merge a string of text and a dynamic variable to produce the value of the 2 combined e.g

    i have a string of text "question" and i have a variable $number the number is in a loop so it increments so i could have

    question1
    question2
    question3

    etc

    but what i want to do is have it display the value of question1 e.g "How do you spell red" and not echo "question1"

    any help would be greatly appreciated :D i have a feeling it has to do with brackets or parenthesis.

    Thanks, -Tim
     
    timallard, Aug 13, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    You mean like...

    
    $question = ${'question' . $number};
    
    PHP:
    ??


    You're better off using arrays though...
     
    nico_swd, Aug 13, 2007 IP
    timallard likes this.
  3. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #3
    Worked perfect, thank you for your time. rep added. yea i wish i used an array from the start ;) uuuuugh lol.
     
    timallard, Aug 13, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    It's never too late to improve your code. :)
     
    nico_swd, Aug 14, 2007 IP
  5. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hey,
    Suppose you need to print from 1 to n questions, you can do:
    
    foreach(range(1,$n) as $num)
    {
         $question = $('question'.$num);
    }
    
    PHP:
    ~
    Thomas
     
    coderlinks, Aug 14, 2007 IP
  6. Kuldeep1952

    Kuldeep1952 Active Member

    Messages:
    290
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #6
    In your original program, if you use $$ (double), it will print what you require.

    e.g.

    echo $$question;

    it will print "How do you spell red"
     
    Kuldeep1952, Aug 14, 2007 IP
  7. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #7
    thank you for future tips ;)
     
    timallard, Aug 14, 2007 IP