Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-23-2005, 02:30 PM   PM User | #1
mtd
Regular Coder

 
Join Date: Jun 2003
Posts: 107
Thanks: 0
Thanked 0 Times in 0 Posts
mtd is an unknown quantity at this point
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!
mtd is offline   Reply With Quote
Old 06-23-2005, 03:06 PM   PM User | #2
mattyod
New Coder

 
Join Date: Feb 2005
Posts: 97
Thanks: 7
Thanked 7 Times in 7 Posts
mattyod is an unknown quantity at this point
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;
?>
mattyod is offline   Reply With Quote
Old 06-23-2005, 03:49 PM   PM User | #3
mrruben5
Regular Coder

 
Join Date: Nov 2004
Location: The Netherlands
Posts: 551
Thanks: 0
Thanked 0 Times in 0 Posts
mrruben5 is an unknown quantity at this point
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"
mrruben5 is offline   Reply With Quote
Old 06-23-2005, 03:52 PM   PM User | #4
delinear
Regular Coder

 
Join Date: Feb 2005
Location: West Midlands, UK
Posts: 623
Thanks: 0
Thanked 0 Times in 0 Posts
delinear is an unknown quantity at this point
Only with double quotes - single quotes as used will treat the $ purely as a part of the string, it shouldn't try to parse.
__________________
~ Bazzy
delinear is offline   Reply With Quote
Old 06-23-2005, 03:53 PM   PM User | #5
mrruben5
Regular Coder

 
Join Date: Nov 2004
Location: The Netherlands
Posts: 551
Thanks: 0
Thanked 0 Times in 0 Posts
mrruben5 is an unknown quantity at this point
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"
mrruben5 is offline   Reply With Quote
Old 06-23-2005, 03:55 PM   PM User | #6
delinear
Regular Coder

 
Join Date: Feb 2005
Location: West Midlands, UK
Posts: 623
Thanks: 0
Thanked 0 Times in 0 Posts
delinear is an unknown quantity at this point
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
__________________
~ Bazzy
delinear is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:27 AM.


Advertisement
Log in to turn off these ads.