Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Phaelon

Thread: Making a variable name with partial use of another variable

  1. #1
    New Coder
    Join Date
    Jan 2014
    Posts
    74
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Making a variable name with partial use of another variable

    Does anyone know how I can make a variable name with partial use of another variable?

    PHP Code:
    while ($row mysql_fetch_assoc($result)) {
        
    $option'.$row['number'].' 1;

    So, I would get:

    $option1 = 1
    $option2 = 1
    $option3 = 1

    I tried '$option'.$row['number'].'' = 1;, but it didn't work.
    dharmend11 likes this.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,851
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    ${'option'.$i}=1;
    See PHP: Variable variables - Manual
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    Phaelon (05-12-2014)

  • #3
    New Coder
    Join Date
    Jan 2014
    Posts
    74
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Thanks so much for your help abduraooft.

    Interpolation! I had a feeling that was the key to my issue and even tried it myself.. but used it way off wrong.

    Where abouts in the manual can I find the answer? I just scanned through it and couldn't see anything on interpolation with variables.

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Posts
    408
    Thanks
    18
    Thanked 26 Times in 26 Posts
    He posted the solution as well, it should work.

    PHP Code:
    while ($row mysql_fetch_assoc($result))
    {
        ${
    'option'.$row['number']} = 1;

    If I've helped you out, show your appreciation by clicking the "Thanks" link as well as a link below!

    AdFly
    Facebook | Twitter
    Google | YouTube


  •  

    Posting Permissions

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