zoukankan      html  css  js  c++  java
  • [PHP] The reference of function

    Assume that we have such function:

    function testRef(&$a = array(10, 20, 30))
    {
    echo "Test of reference.\n";
    }

    When we use it as below:

    代码
    /*
    *
    *Example 1
    *
    */
    testRef(
    array(10, 30, 40));
    //The out is :
    //Fatal error: Only variables can be passed by reference



    /*
    *
    *Example 2
    *
    */
    $a = array(10, 30, 40);
    testRef(
    $a);
    //The out is :
    //Test of reference.

    If we remove the "&" from the function,

    function testRef($a = array(10, 20, 30))
    {
    echo "Test of reference.\n";
    }

    The same test code:

    代码
    /*
    *
    *Example 3
    *
    */
    testRef(
    array(10, 30, 40));
    //The out is :
    //Test of reference.



    /*
    *
    *Example 4
    *
    */
    $a = array(10, 30, 40);
    testRef(
    $a);
    //The out is :
    //Test of reference.

    So we got the note that, when using reference in function, we have first set the value to a variable, then call the function with the variable.

  • 相关阅读:
    redis安装及简单命令
    struts2 第二天
    初学struts2-入门案列
    hibernate第二天
    hibernate入门
    同义词,索引,表分区
    表空间,序列
    orcale函数
    orcale错题分析
    orcale开篇
  • 原文地址:https://www.cnblogs.com/davidhhuan/p/1736062.html
Copyright © 2011-2022 走看看