0

I'm not sure how to write this. I have a variable called $group the value could be a,b or c. I also have variables called $a, $b and $c. How do I write something that states if $group=a then use $a, if $group=b then use $b and if $group=c then use $c. Hope that makes sense.

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54

2 Answers2

0

Do you want this ??

$a = "a"; // $a is variable and "a" is its value and its a string.

$b = "b";

$c = "c";


if ($group == "a")  // here a is just string value not variable. 
{
    echo $a;
}
elseif ($group == "b")
{
    echo $b;
}
else
{
    echo $c;
}

Or

Variable of variables

Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically. A normal variable is set with a statement such as:

<?php
$a = 'hello';
?>

A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, hello, can be used as the name of a variable by using two dollar signs. i.e.

<?php
$$a = 'world';
?>

At this point two variables have been defined and stored in the PHP symbol tree: $a with contents "hello" and $hello with contents "world". Therefore, this statement:

<?php
echo "$a ${$a}";
?>

produces the exact same output as:

<?php
echo "$a $hello";
?>

i.e. they both produce: hello world.

For more info click here

Community
  • 1
  • 1
Rahul
  • 763
  • 1
  • 12
  • 45
0

With Rahul's help and a tiny bit of tweaking this was my working end result.

if ($group == "a") 
{
    $group=$a;
}
elseif ($group == "b")
{
    $group=$b;
}
else
{
    $group=$c;
}