Created
September 14, 2017 13:19
-
-
Save kuntalchandra/66a95f8bc7e7f6d7072cb47f66b3a30d to your computer and use it in GitHub Desktop.
How variable reference works in PHP: Deep or shallow copy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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