Skip to content

Instantly share code, notes, and snippets.

@kuntalchandra
Created September 14, 2017 13:19
Show Gist options
  • Save kuntalchandra/66a95f8bc7e7f6d7072cb47f66b3a30d to your computer and use it in GitHub Desktop.
Save kuntalchandra/66a95f8bc7e7f6d7072cb47f66b3a30d to your computer and use it in GitHub Desktop.
How variable reference works in PHP: Deep or shallow copy
<?php
//example of shallow copy
$arr = array(1, 2);
$arr1 = &$arr;
$arr1[0]++;
echo "\nCopied by reference\n";
echo $arr[0], "\n";
echo $arr1[0], "\n";
unset($arr, $arr1);
//example of deep copy
$arr = array(1, 2);
$arr1 = $arr;
$arr1[0]++;
echo "\nCopied by Value\n";
echo $arr[0], "\n";
echo $arr1[0], "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment