Hướng dẫn how do you reference a variable in php? - làm thế nào để bạn tham chiếu một biến trong 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:

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 ¶

8 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

Nickshanks tại Nickshanks Dot Com ¶

5 năm trước

foo[$a]2

foo[$a]3

foo[$a]4

foo[$a]5

Rob tại Librobert Dot Net

10 tháng trước

foo[$a]6

foo[$a]7

foo[$a]8

foo[$a]9

0

1

foo[$a]1

Jason Steelman ¶

2 năm trước

3

4

foo[$a]1

PHPNet tại Holodyn dot com ¶

8 năm trước

6

7

foo[$a]1

ccb_bc tại hotmail dot com ¶

3 năm trước

9

0

1

2

3

4

Mike tại Eastghost Dot Com ¶

2 năm trước

5

6

7

8

foo[$a]1

PHPNet tại Holodyn dot com ¶

diabolos @t gmail 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. 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

10 năm trước

Yiangforwork tại Gmail 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

Tianyiw tại VIP Dot qq dot com ¶

5 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

Jason Steelman ¶

5 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

Làm thế nào để bạn tham khảo một biến?

Các biến tham chiếu..
Một biến tham chiếu là một biệt danh hoặc bí danh, cho một số biến khác ..
Để xóa một biến tham chiếu, chúng tôi sử dụng toán tử Unary & int n = 5; // Điều này tuyên bố một biến, n int & r = n; // Điều này tuyên bố r là một tham chiếu đến n. ....
Để khai báo một biến tham chiếu, hãy thêm & toán tử sau loại ..

Làm thế nào để bạn tham khảo một lớp trong PHP?

Bạn chỉ có thể làm như vậy, vì bạn chỉ mới được khởi tạo một lần, bạn có thể tham khảo trường hợp đó với biến thể hiện $.Ngoài ra, bạn cũng có thể "tham chiếu" phiên bản $ đó trong một số biến khác: $ tham chiếu = $ cá thể;Bây giờ bạn có thể truy cập phiên bản duy nhất của lớp với phiên bản $ và biến tham chiếu $.refer to that instance with the $instance variable. Additionally you can "reference" that $instance as well in some other variable: $reference = $instance; You can now access the single instance of Class with the $instance and the $reference variable.

$ _ Trong PHP là gì?

Tên biến tuân theo các quy tắc giống như các nhãn khác trong PHP.Một tên biến hợp lệ bắt đầu bằng một chữ cái hoặc dấu gạch dưới, theo sau là bất kỳ số lượng chữ cái, số hoặc dấu gạch dưới.Vì vậy, $ _ chỉ là một biến tùy ý.an arbitrary variable.

PHP có vượt qua tham chiếu hoặc giá trị không?

Đó là theo giá trị theo tài liệu PHP.Theo mặc định, các đối số hàm được truyền theo giá trị [để nếu giá trị của đối số trong hàm được thay đổi, nó không bị thay đổi bên ngoài hàm].Để cho phép một hàm sửa đổi các đối số của nó, chúng phải được truyền bằng cách tham chiếu.by value according to the PHP Documentation. By default, function arguments are passed by value [so that if the value of the argument within the function is changed, it does not get changed outside of the function]. To allow a function to modify its arguments, they must be passed by reference.

Bài Viết Liên Quan

Chủ Đề