Created
November 18, 2014 09:47
-
-
Save fmtarif/bfc99eda91fde9964fd8 to your computer and use it in GitHub Desktop.
#php Array assignment by reference or by value dilemma
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 | |
| //Array assignment always involves value copying. Use the reference operator to copy an array by reference. | |
| //http://php.net/manual/en/language.types.array.php#example-109 | |
| $email = array(); | |
| $code = array(); | |
| $res = array( | |
| 'email' => $email //use &$email | |
| ,'code' => $code //use &$code | |
| ); | |
| echo "before update"; | |
| print_r($res); | |
| $email = array('status' => 'ok'); | |
| $code = array('status' => 'err'); | |
| echo "after update"; | |
| print_r($res); //this will output "email" key of $res as empty becuase it was assigned by value during init in line 7 not by reference | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment