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 6 of 6
  1. #1
    mtd
    mtd is offline
    Regular Coder
    Join Date
    Jun 2003
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    combining variables

    Is there a way to define one variable's value as the names (not values) of two other variables?

    i.e.
    $var1 = 'ONE';
    $var2 = 'TWO';

    New variable ($var3) needs to be named '$var1var2'. A simple combo like:

    $var3 = $var1.$var2;

    works with the values of $var1 and $var2, yielding "ONETWO". So that doesn't work.

    Maybe this is really easy? really hard? against the nature of PHP? I figured there's got to be a way!

  • #2
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts
    Hmm maybe I'm missing the point of your question but simply.

    $var3 = '$var1'.'$var2';

    will return the value you want i.e $var1$var2 but you are just passing in a string not doing anything dynamic. So this would work too:

    $var3 = '$var1$var2';

    Maybe I'm missing the point but I don't see why you are giving variables to $var1 & $var2 in order to declare another variable based on thier names. By which I mean that
    Code:
    <?php
    $var1 = 'ONE';
    $var2 = 'TWO';
    $var3 = '$var1$var2';
    
    echo $var3;
    ?>
    will return the same as:
    Code:
    <?php
    $var3 = '$var1$var2';
    
    echo $var3;
    ?>

  • #3
    Regular Coder
    Join Date
    Nov 2004
    Location
    The Netherlands
    Posts
    551
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Shouldn't the dollar signs be escaped?
    PHP Code:
    $var1 'ONE';
    $var2 'TWO';
    $var3 '\$var1\$var2'
    CATdude about IE6: "All your box-model are belong to us"

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Only with double quotes - single quotes as used will treat the $ purely as a part of the string, it shouldn't try to parse.

  • #5
    Regular Coder
    Join Date
    Nov 2004
    Location
    The Netherlands
    Posts
    551
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So that's why my \n line breaks don't work with single quotes huh
    CATdude about IE6: "All your box-model are belong to us"

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yep I had that problem a lot when I first started out, wondering why I had \n's all over everything. The answer is always a lot more simple than you'd expect


  •  

    Posting Permissions

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