php - 将变量(数组)分配给其他变量不会从源变量中获取副本
问题描述
假设这样的代码:
$a = [1,2];
$b = &$a; // $b holds reference to $a
$b[] = 3; // so this makes $a to {1,2,3}
$b = $a; // $b now holds COPY of $a (not reference)
$b[] = 4; // so $a must be the same {1,2,3}
var_dump($a); // but it isn't - $a is {1,2,3,4}
因此,不知何故,语句$b = $a;
并没有按照应有的方式分配 $a 数组的新副本,而是仅使用它的引用。这是为什么 ?
解决方案
推荐阅读
- python - 在 MYSql 中使用 Python 更新数据时,出现错误
- ruby-on-rails - 使用 tabs_on_rails 进行 Rails 选项卡导航
- python - 如何在 Raspberry Pi 上处理 linux 中的子进程?
- go - 如何使用 pfx 证书设置 httpClient?
- string - 当我安装“STRINGSIFTER”时,出现错误
- javascript - 如何在文本输入的占位符中赋予多种颜色反应本机
- sql - 如何在 AZURE DW 或 Azure 突触中制作用户或角色特定的触发器?
- html - HTML 表单是个谜
- linux-kernel - 5.4.0-59 和 5.4.59 内核有什么不同?
- ide - 适合小型企业的 IDE