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.

  • 相关阅读:
    JDBC
    Servlet的优缺点
    css样式,媒体查询,垂直居中,js对象
    浏览器渲染优化
    css秘密花园
    express 4
    redux
    koa
    webpack
    react学习总结
  • 原文地址:https://www.cnblogs.com/davidhhuan/p/1736062.html
Copyright © 2011-2022 走看看