Hướng dẫn php function use reference - tham chiếu sử dụng hàm php
Bạn có thể chuyển một biến bằng cách tham chiếu đến một hàm để hàm có thể sửa đổi biến. Cú pháp như sau:
Những điều sau đây có thể được thông qua bằng cách tham khảo:
Không có biểu thức nào khác nên được truyền qua tham chiếu, vì kết quả không được xác định. Ví dụ: các ví dụ sau về việc truyền qua tham chiếu không hợp lệ:
Tnestved tại Yahoo Dot Com ¶ ¶ 7 năm trước
ccb_bc tại hotmail dot com ¶ ¶ 3 năm trước
$x = 10; Mike tại Eastghost Dot Com ¶ ¶ 7 năm trước
foo($a) 1ccb_bc tại hotmail dot com ¶ ¶ 3 năm trước
foo($a) 5Mike tại Eastghost Dot Com ¶ ¶ Nickshanks tại Nickshanks Dot Com ¶
foo($a) 15 năm trước ¶ Rob tại Librobert Dot Net
foo($a) 110 tháng trước ¶ Jason Steelman ¶
foo($a) 12 năm trước ¶ PHPNet tại Holodyn dot com ¶
function foo(&$var) 48 năm trước ¶ Rob tại Librobert Dot Net
foo($a) 110 tháng trước ¶ Jason Steelman ¶
By removing the ability to include the reference sign on function calls where pass-by-reference is incurred (I.e., function definition uses &), the readability of the code suffers, as one has to look at the function definition to know if the variable being passed is by-ref or not (I.e., potential to be modified). If both function calls and function definitions require the reference sign (I.e., &), readability is improved, and it also lessens the potential of an inadvertent error in the code itself. Going full on fatal error in 5.4.0 now forces everyone to have less readable code. That is, does a function merely use the variable, or potentially modify it...now we have to find the function definition and physically look at it to know, whereas before we would know the intent immediately.
32 năm trước ¶ PHPNet tại Holodyn dot com ¶
By removing the ability to include the reference sign on function calls where pass-by-reference is incurred (I.e., function definition uses &), the readability of the code suffers, as one has to look at the function definition to know if the variable being passed is by-ref or not (I.e., potential to be modified). If both function calls and function definitions require the reference sign (I.e., &), readability is improved, and it also lessens the potential of an inadvertent error in the code itself. Going full on fatal error in 5.4.0 now forces everyone to have less readable code. That is, does a function merely use the variable, or potentially modify it...now we have to find the function definition and physically look at it to know, whereas before we would know the intent immediately.
98 năm trước ¶ 3 năm trước
foo($a) 1Mike tại Eastghost Dot Com ¶ ¶ 7 năm trước
foo($a) 1ccb_bc tại hotmail dot com ¶ ¶ 3 năm trước
// Here we use the 'use' operator to create a variable within the scope of the function. Although it may seem that the newly created variable has something to do with '$x' that is outside the function, we are actually creating a '$x' variable within the function that has nothing to do with the '$x' variable outside the function. We are talking about the same names but different content locations in memory. 1Mike tại Eastghost Dot Com ¶ ¶ Nickshanks tại Nickshanks Dot Com ¶
foo($a) 15 năm trước ¶ Nickshanks tại Nickshanks Dot Com ¶
// Now the magic happens with using the reference (&). Now we are actually accessing the contents of the '$y' variable that is outside the scope of the function. All the actions that we perform with the variable '$y' within the function will be reflected outside the scope of this same function. Remembering this would be an impure function in the functional paradigm, since we are changing the value of a variable by reference. $x = 10; 15 năm trước ¶ 3 năm trước // Now the magic happens with using the reference (&). Now we are actually accessing the contents of the '$y' variable that is outside the scope of the function. All the actions that we perform with the variable '$y' within the function will be reflected outside the scope of this same function. Remembering this would be an impure function in the functional paradigm, since we are changing the value of a variable by reference. // Now the magic happens with using the reference (&). Now we are actually accessing the contents of the '$y' variable that is outside the scope of the function. All the actions that we perform with the variable '$y' within the function will be reflected outside the scope of this same function. Remembering this would be an impure function in the functional paradigm, since we are changing the value of a variable by reference. // Now the magic happens with using the reference (&). Now we are actually accessing the contents of the '$y' variable that is outside the scope of the function. All the actions that we perform with the variable '$y' within the function will be reflected outside the scope of this same function. Remembering this would be an impure function in the functional paradigm, since we are changing the value of a variable by reference. foo($a) 1 |