Reputation: 719
How shall I store two variable into one?
Here is an example,
$a = 'hello';
$b = 'hola';
A way is array like,
$c = array('a', 'b');
$myvariables = $c;
echo $myvariables[0];
but how can I preserve the values of $a & $b into $c without using array something like
$c = ($a + $b)?
Upvotes: 1
Views: 8913
Reputation: 9226
If you need the values in a string and split them later, you may define a delimitter (Like | or ; ) and String your values together.
$a = "hello";
$b = "world";
$delimitter = "|";
$array = array($a,$b);
// add new values
array_push($array, "Get", $b);
$stringlist = implode($delimitter, $array);
// $stringlist = "hello|world|Get|world";
// and to get them back
$array = explode($stringlist,$delimitter);
// you may even directly access a value:
$a= explode($stringlist,$delimitter)[0];
Upvotes: 0
Reputation: 375
When storing variables, there is a difference when you use single quotes and double quotes. If you use single quotes, the variable names stay intact. If you use double quotes, however, PHP will interpret them as variables. Here is an example:
$a = "hello";
$b = "world";
$c = '$a $b';
echo $c;
$d = "$a $b";
echo "</br>";
echo $d;
This will output:
$a $b
hello world
Upvotes: 0