PHP execution function when assigned to a variable

I have a function that 'echos' before returning an array. When assigning that function to a variable like below, it executes the function outputting the echo. Is this expected behavior as I thought assigning to a variable should only do just that, assign? Thanks

 function check($link) {
 //.....
  echo "some text ";
 return $array;

}

 $test = check($link);

When you assign a function to a variable like that what php is actually doing is executing the function to get its return value and then assigning that value to the variable.

So php executes your function (and therefore does the echo statement) and then assigns the return value ($array) to $test.


it executes line by line so when it gets echo "some text "; it prints and after that it returns the array.


Yes, it is expected behavior. You are creating a side effect(echoing) while also assigning to a variable. You are doing what is called a query and a command in the same time, which is totally legitimate.

链接地址: http://www.djcxy.com/p/58900.html

上一篇: 和! 运营商足以让每一个可能的逻辑表达?

下一篇: PHP执行函数分配给一个变量时