Page 1 of 1

Using two variables to create one variable.

Posted: Fri Oct 18, 2002 7:27 pm
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...

Posted: Fri Oct 18, 2002 7:56 pm
by volka

Posted: Fri Oct 18, 2002 8:03 pm
by Bennettman
I... have... no idea how to implement that... ;p

Posted: Fri Oct 18, 2002 8:35 pm
by volka
try this one

Code: Select all

<?php
$test1 = 'text';

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

echo ${$var.$num};
?>

Posted: Sat Oct 19, 2002 5:07 am
by Bennettman
Mmkay, I'll try that.

Posted: Sat Oct 19, 2002 7:49 am
by Takuma
How about this...

It's much simpler.

Code: Select all

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

$new = $id.$value;

echo $new
?>

Posted: Sat Oct 19, 2002 11:24 am
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)

Posted: Sat Oct 19, 2002 5:35 pm
by Bennettman
Thanks, Volka's one worked fine ^_^

I learned a new trick today! ;p

Posted: Sat Oct 19, 2002 6:38 pm
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.

Posted: Sun Oct 20, 2002 3:31 am
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....