Hướng dẫn are php variables passed by reference? - có phải các biến php được truyền bằng tham chiếu không?

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:

Lưu ý: Không có dấu hiệu tham chiếu trên lệnh gọi hàm - chỉ trên các định nghĩa chức năng. Định nghĩa chức năng một mình là đủ để vượt qua chính xác đối số bằng cách tham khảo.: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference.

Những điều sau đây có thể được thông qua bằng cách tham khảo:

  • Biến, tức là foo[$a]
  • Các tài liệu tham khảo được trả về từ các chức năng, tức là:

    Xem thêm về trả lại 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

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.

ccb_bc tại hotmail dot com ¶

3 năm trước

Mike tại Eastghost Dot Com ¶

7 năm trước

beware unset[]  destroys references

$x = 'x';
change[ $x ];
echo $x; // outputs "x" not "q23"  ---- remove the unset[] and output is "q23" not "x"

foo[$a]0

foo[$a]1

ccb_bc tại hotmail dot com ¶

3 năm trước

foo[$a]2

foo[$a]3

foo[$a]4

foo[$a]5

Mike tại Eastghost Dot Com ¶

Nickshanks tại Nickshanks Dot Com ¶

foo[$a]6

foo[$a]7

foo[$a]8

foo[$a]9

0

1

foo[$a]1

5 năm trước

Rob tại Librobert Dot Net

3

4

foo[$a]1

10 tháng trước

Jason Steelman ¶

6

7

foo[$a]1

2 năm trước

PHPNet tại Holodyn dot com ¶

9

0

1

2

3

4

8 năm trước

Rob tại Librobert Dot Net

5

6

7

8

foo[$a]1

10 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. 0

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. 1

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. 2

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. 3

2 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. 4

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. 5

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. 6

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. 7

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. 8

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. 9

8 năm trước

3 năm trước

0

$x = 10;
[function[] use [
$x]{
   
$x = $x*$x;
   
var_dump[$x]; // 100
}][];
var_dump[$x]; // 10

// 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.

$y = 10;
[function[] use [&
$y]{
   
$y = $y*$y;
   
var_dump[$y]; // 100
}][];
var_dump[$y]; // 100
?>
1

5 năm trước

3 năm trước

$x = 10;
[function[] use [
$x]{
   
$x = $x*$x;
   
var_dump[$x]; // 100
}][];
var_dump[$x]; // 10

// 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.

$y = 10;
[function[] use [&
$y]{
   
$y = $y*$y;
   
var_dump[$y]; // 100
}][];
var_dump[$y]; // 100
?>
2

$x = 10;
[function[] use [
$x]{
   
$x = $x*$x;
   
var_dump[$x]; // 100
}][];
var_dump[$x]; // 10

// 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.

$y = 10;
[function[] use [&
$y]{
   
$y = $y*$y;
   
var_dump[$y]; // 100
}][];
var_dump[$y]; // 100
?>
3

$x = 10;
[function[] use [
$x]{
   
$x = $x*$x;
   
var_dump[$x]; // 100
}][];
var_dump[$x]; // 10

// 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.

$y = 10;
[function[] use [&
$y]{
   
$y = $y*$y;
   
var_dump[$y]; // 100
}][];
var_dump[$y]; // 100
?>
4

foo[$a]1

Các đối tượng trong Php 5 được truyền bởi giá trị hoặc tham chiếu giải thích?

Một trong những điểm chính của PHP OOP thường được đề cập là "các đối tượng được truyền qua các tài liệu tham khảo theo mặc định". Điều này không hoàn toàn đúng. Phần này điều chỉnh suy nghĩ chung đó bằng một số ví dụ. Tham chiếu PHP là bí danh, cho phép hai biến khác nhau ghi vào cùng một giá trị.

Bạn có thể vượt qua một biến bằng cách tham khảo?

Truyền qua tham chiếu là một cái gì đó mà các nhà phát triển C ++ sử dụng để cho phép một hàm sửa đổi một biến mà không phải tạo một bản sao của nó.Để truyền một biến theo tham chiếu, chúng tôi phải khai báo các tham số chức năng là tham chiếu và không phải là các biến bình thường.To pass a variable by reference, we have to declare function parameters as references and not normal variables.

Những gì được thông qua trong một cuộc gọi PHP bằng cách tham khảo?

Truyền qua tham chiếu: Khi các biến được truyền bằng biểu tượng tham chiếu, sử dụng & [ampersand] cần được thêm vào trước khi đối số biến.Ví dụ: Hàm [& $ x].Phạm vi của cả biến toàn cầu và chức năng trở thành toàn cầu vì cả hai biến được xác định bởi cùng một tham chiếu.use & [ampersand] symbol need to be added before variable argument. For example: function[ &$x ]. Scope of both global and function variable becomes global as both variables are defined by same reference.

Tham chiếu biến trong PHP là gì?

Trong PHP, tên biến và nội dung biến là khác nhau, vì vậy cùng một nội dung có thể có tên khác nhau.Một biến tham chiếu được tạo bằng tiền tố & ký vào biến gốc.Do đó b = & a sẽ có nghĩa là bisareferewncevarableofa.A reference variable is created by prefixing & sign to original variable. Hence b=&a will mean that bisareferewncevariableofa.

Bài Viết Liên Quan

Chủ Đề