Skip to content

Instantly share code, notes, and snippets.

@fmtarif
Created November 18, 2014 09:47
Show Gist options
  • Select an option

  • Save fmtarif/bfc99eda91fde9964fd8 to your computer and use it in GitHub Desktop.

Select an option

Save fmtarif/bfc99eda91fde9964fd8 to your computer and use it in GitHub Desktop.
#php Array assignment by reference or by value dilemma
<?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