PDA

View Full Version : set $var from a $var



kabatak
November 17th, 2002, 02:22
Is it posible to set a $var from a $var like

$a = hello;
$b = there;

$$a$b = hi;

so that

$hellothere will have the value of "hi"

hohoho
November 17th, 2002, 02:47
it is possible :D
<?
$a = "hello";
$b = "there";

$var="$a$b";
$$var="hi";
echo $hellothere;
?>

Explanation: if there is a $var and you set $$var, then there will be a new variable called like the content of $var and the value of $$var

btw: nice post count ben (666) ;)

kabatak
November 17th, 2002, 03:09
1st I thought it like this:

$a = "hello";
$b = "there";
$var="ab";
$$var="hi";
echo $var;

because of:

$a = "hello";
$var = "a";
echo $$var;

obviously didn't work, but I didn't thought of that method of yours hohoho, thank you, it works :)

kabatak
November 17th, 2002, 03:36
I have another problem

$a = "hello";
$b = "there";
$var="$a$b";
$$var="hi";
echo $hellothere;

What if the value of $$var is coming from a form, how do you set its value?

hohoho
November 17th, 2002, 03:37
$$var = $formvar

kabatak
November 17th, 2002, 03:53
but how, if the form var is $hellothere

$a = "hello";
$b = "there";
$var="$a$b";
$$var="hi";
echo $hellothere;

hohoho
November 17th, 2002, 03:55
then you already have the var $hellothere...
just rename the $var

kabatak
November 17th, 2002, 03:59
confused... uggg :confused: :crysad:

Kaliber
November 17th, 2002, 04:04
use the [ php ] bbcode

kabatak
November 17th, 2002, 06:35
ok, I think I got it. what I actually want was someting like

$a = "hello";
$b = "there";
$var="$a$b";
$$var='$_POST["$a$b"]';
echo ("$a$b");

still confused.:confused2

biggulp
November 17th, 2002, 22:14
don't think you need quotes around variable names.

$a = "hello";
$b = "there";
$var=$a . $b;
$$var = $_POST[{$a}{$b}];
echo $a . $b;