Klanto Aguntuk
Klanto Aguntuk

Reputation: 719

Storing two variables into one

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

Answers (3)

Xavjer
Xavjer

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

ashin999
ashin999

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

Daan
Daan

Reputation: 12236

Do you mean concatenation?

$c = $a.$b;

Upvotes: 7

Related Questions