SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru JoePrice's Avatar
    Join Date
    Jan 2002
    Location
    UK
    Posts
    673
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    combining a string and a variable to become the name of a new variable ?

    Hi there

    I'm trying to create a variable which comprises a string and a variable and am wondering how i do this?

    basically I want to do $question4 where 'question' is a string and the 4 would be $i

    $['question'.$i] doesn't work...

    many thanks for any suggestions...

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    153 Post(s)
    Tagged
    3 Thread(s)
    $question.$i
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    spain
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    maybe in this way:

    PHP Code:
    $i=1;
    $question1='Hello World';
    $myVar='question'.$i;
    echo $
    $myVar

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The syntax you're looking for is:

    PHP Code:
    $question4 "foo";
    $i 4;
    echo ${
    "question$i"}; 
    But the natural (and preferred) way to do this is to use arrays:

    PHP Code:
    $question[4] = "foo";
    $i 4;
    echo 
    $question[$i]; 

  5. #5
    SitePoint Guru JoePrice's Avatar
    Join Date
    Jan 2002
    Location
    UK
    Posts
    673
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    great thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •