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.

相关文章:

  • 2021-10-14
  • 2021-05-17
  • 2022-12-23
  • 2022-02-04
  • 2021-11-09
  • 2021-08-07
  • 2021-09-25
  • 2021-07-11
猜你喜欢
  • 2021-06-24
  • 2021-10-06
  • 2022-12-23
  • 2021-08-26
  • 2021-06-04
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案