How does PHP handle the copying of data in memory when using references, and what impact does it have on memory efficiency?

When using references in PHP, instead of creating a new copy of the data in memory, PHP simply creates a reference to the original data. This means that any changes made to the reference will affect the original data as well. This can improve memory efficiency by reducing the amount of memory needed to store duplicate data.

$data = [1, 2, 3];

// Using references to avoid copying data
$reference = &$data;

// Modify the reference
$reference[] = 4;

print_r($data); // Output: [1, 2, 3, 4]