【问题标题】:When should I return a reference from a function我什么时候应该从函数返回引用
【发布时间】:2017-06-06 20:46:24
【问题描述】:

我正在阅读关于 Returning values 的 PHP 文档。老实说,我无法理解 Example #3 是如何工作的。或者我什么时候应该使用这样的模板。

<?php
function &returns_reference()
{
    return $someref;
}

$newref =& returns_reference();

我已经执行了它online,但它什么也没返回。无论如何,你能告诉我什么时候应该在实践中使用它吗?或者至少给我一个更清楚的例子?

【问题讨论】:

标签: php function variables reference pass-by-reference


【解决方案1】:

demo here

<?php
    $someref = 3;
    function &returns_reference(&$someref)
    {
        return $someref;
    }

    $var = &returns_reference($someref);
    $var = 5;
    echo $someref;

【讨论】:

  • 好奇者和好奇者!
  • @stack 你会检查它。
猜你喜欢
  • 2019-07-08
  • 2020-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多