Skip to content
PHP Logo

How can you link one variable to another in PHP without just inserting it?

When you want to link one variable to another in PHP without just inserting it, you can use references. By assigning one variable to another using the "&" symbol, changes made to one variable will also affect the other. This can be useful when you want to manipulate the same data in different ways without duplicating it.

php
$var1 = 'Hello';
$var2 = &$var1; // $var2 is now a reference to $var1

echo $var1; // Output: Hello
echo $var2; // Output: Hello

$var1 = 'Goodbye';

echo $var1; // Output: Goodbye
echo $var2; // Output: Goodbye