Using two variables to create one variable.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Using two variables to create one variable.

Post by Bennettman »

Is it possible to specify a variable made up of the names/values of two variables?

For example:

Code: Select all

$id1 = ("hello");
$value = 1;

echo ($id$value);
What I'm trying to do is have it treat $id as "$id" and not the value of $id; then treat $id$value as one whole variable, made up of "$id" and the value of $value, i.e. $id1. Therefore the wanted end result for the code above would be an echo of "hello" - the string of $id1.

I'm totally stumped here...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

I... have... no idea how to implement that... ;p
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try this one

Code: Select all

<?php
$test1 = 'text';

$var = 'test';
$num = 1;

echo ${$var.$num};
?>
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

Mmkay, I'll try that.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

How about this...

It's much simpler.

Code: Select all

<?php
$id = "hello";
$value = 1;

$new = $id.$value;

echo $new
?>
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

Takuma wrote:It's much simpler.
Simpler...
Different alright.
Heres the test code:

Code: Select all

Takuma:<BR>
<?
$id = "hello";
$value = 1;

$new = $id.$value;

echo $new;

?>
<br>Volka:<br>
<?

$test1 = 'text';

$var = 'test';
$num = 1;

echo ${$var.$num};
?>
And here's what was output:

Code: Select all

Takuma:
hello1
Volka:
text
8)
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

Thanks, Volka's one worked fine ^_^

I learned a new trick today! ;p
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

This way of naming variables is what I like most about PHP.
You can do stuff that has variable names that only PHP knows about...

Is that good? I here you ask.
Well, If you just know the names can't be manipulated by hacking the place, you can do wonderful dynamic database driven things using a few lines of PHP-code.

THAT really makes advanced scripting worth its time.
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

yeah
its also kinda handy if you dont know how many times a thing needs to be done...

to elaborate, something like this:

Code: Select all

<?php
for($k=0;$k<$i;$k++){
	$temp = 'player' . $k;
	$$temp = new play;
	$$temp->user = $usersї$k];
	$$temp->fill();
	}
?>
and then use the same thing to access the objects....
Post Reply