Hướng dẫn what is the difference between accessing a variable by name and by reference php? - sự khác biệt giữa truy cập một biến theo tên và tham chiếu php là gì?

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:

<?php
function foo(&$var)
{
    
$var++;
}
$a=5;
foo($a);
// $a is 6 here
?>

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à:

    <?php
    function foo(&$var)
    {
        
    $var++;
    }
    function &
    bar()
    {
        
    $a 5;
        return 
    $a;
    }
    foo(bar());
    ?>

    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ệ:

<?php
function foo(&$var)
{
    
$var++;
}
function 
bar() // Note the missing &
{
    
$a 5;
    return 
$a;
}
foo(bar()); // Produces a noticefoo($a 5); // Expression, not variable
foo(5); // Produces fatal errorclass Foobar
{
}
foo(new Foobar()) // Produces a notice as of PHP 7.0.7
                  // Notice: Only variables should be passed by reference
?>

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

<?php
// PHP >= 5.6

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

$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
?>

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

<?php
function foo(&$var)
{
    
$var++;
}
function &
bar()
{
    
$a 5;
    return 
$a;
}
foo(bar());
?>
0

<?php
function foo(&$var)
{
    
$var++;
}
function &
bar()
{
    
$a 5;
    return 
$a;
}
foo(bar());
?>
1

foo($a)1

5 năm trước

Rob tại Librobert Dot Net

<?php
function foo(&$var)
{
    
$var++;
}
function &
bar()
{
    
$a 5;
    return 
$a;
}
foo(bar());
?>
3

<?php
function foo(&$var)
{
    
$var++;
}
function &
bar()
{
    
$a 5;
    return 
$a;
}
foo(bar());
?>
4

foo($a)1

10 tháng trước

Jason Steelman ¶

<?php
function foo(&$var)
{
    
$var++;
}
function &
bar()
{
    
$a 5;
    return 
$a;
}
foo(bar());
?>
6

<?php
function foo(&$var)
{
    
$var++;
}
function &
bar()
{
    
$a 5;
    return 
$a;
}
foo(bar());
?>
7

foo($a)1

2 năm trước

PHPNet tại Holodyn dot com ¶

<?php
function foo(&$var)
{
    
$var++;
}
function &
bar()
{
    
$a 5;
    return 
$a;
}
foo(bar());
?>
9

<?php
function foo(&$var)
{
    
$var++;
}
function 
bar() // Note the missing &
{
    
$a 5;
    return 
$a;
}
foo(bar()); // Produces a noticefoo($a 5); // Expression, not variable
foo(5); // Produces fatal errorclass Foobar
{
}
foo(new Foobar()) // Produces a notice as of PHP 7.0.7
                  // Notice: Only variables should be passed by reference
?>
0

<?php
function foo(&$var)
{
    
$var++;
}
function 
bar() // Note the missing &
{
    
$a 5;
    return 
$a;
}
foo(bar()); // Produces a noticefoo($a 5); // Expression, not variable
foo(5); // Produces fatal errorclass Foobar
{
}
foo(new Foobar()) // Produces a notice as of PHP 7.0.7
                  // Notice: Only variables should be passed by reference
?>
1

<?php
function foo(&$var)
{
    
$var++;
}
function 
bar() // Note the missing &
{
    
$a 5;
    return 
$a;
}
foo(bar()); // Produces a noticefoo($a 5); // Expression, not variable
foo(5); // Produces fatal errorclass Foobar
{
}
foo(new Foobar()) // Produces a notice as of PHP 7.0.7
                  // Notice: Only variables should be passed by reference
?>
2

<?php
function foo(&$var)
{
    
$var++;
}
function 
bar() // Note the missing &
{
    
$a 5;
    return 
$a;
}
foo(bar()); // Produces a noticefoo($a 5); // Expression, not variable
foo(5); // Produces fatal errorclass Foobar
{
}
foo(new Foobar()) // Produces a notice as of PHP 7.0.7
                  // Notice: Only variables should be passed by reference
?>
3

<?php
function foo(&$var)
{
    
$var++;
}
function 
bar() // Note the missing &
{
    
$a 5;
    return 
$a;
}
foo(bar()); // Produces a noticefoo($a 5); // Expression, not variable
foo(5); // Produces fatal errorclass Foobar
{
}
foo(new Foobar()) // Produces a notice as of PHP 7.0.7
                  // Notice: Only variables should be passed by reference
?>
4

8 năm trước

Rob tại Librobert Dot Net

<?php
function foo(&$var)
{
    
$var++;
}
function 
bar() // Note the missing &
{
    
$a 5;
    return 
$a;
}
foo(bar()); // Produces a noticefoo($a 5); // Expression, not variable
foo(5); // Produces fatal errorclass Foobar
{
}
foo(new Foobar()) // Produces a notice as of PHP 7.0.7
                  // Notice: Only variables should be passed by reference
?>
5

<?php
function foo(&$var)
{
    
$var++;
}
function 
bar() // Note the missing &
{
    
$a 5;
    return 
$a;
}
foo(bar()); // Produces a noticefoo($a 5); // Expression, not variable
foo(5); // Produces fatal errorclass Foobar
{
}
foo(new Foobar()) // Produces a notice as of PHP 7.0.7
                  // Notice: Only variables should be passed by reference
?>
6

<?php
function foo(&$var)
{
    
$var++;
}
function 
bar() // Note the missing &
{
    
$a 5;
    return 
$a;
}
foo(bar()); // Produces a noticefoo($a 5); // Expression, not variable
foo(5); // Produces fatal errorclass Foobar
{
}
foo(new Foobar()) // Produces a notice as of PHP 7.0.7
                  // Notice: Only variables should be passed by reference
?>
7

<?php
function foo(&$var)
{
    
$var++;
}
function 
bar() // Note the missing &
{
    
$a 5;
    return 
$a;
}
foo(bar()); // Produces a noticefoo($a 5); // Expression, not variable
foo(5); // Produces fatal errorclass Foobar
{
}
foo(new Foobar()) // Produces a notice as of PHP 7.0.7
                  // Notice: Only variables should be passed by reference
?>
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

<?php
// PHP >= 5.6
0

<?php
// PHP >= 5.6
1

<?php
// PHP >= 5.6
2

foo($a)1

Mike tại Eastghost Dot Com ¶

7 năm trước

<?php
// PHP >= 5.6
4

<?php
// PHP >= 5.6
5

<?php
// PHP >= 5.6
6

foo($a)1

ccb_bc tại hotmail dot com ¶

3 năm trước

<?php
// PHP >= 5.6
8

<?php
// PHP >= 5.6
9

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

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

Mike tại Eastghost Dot Com ¶

Nickshanks tại Nickshanks Dot Com ¶

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

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

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

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

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

foo($a)1

5 năm trước

Nickshanks tại Nickshanks Dot Com ¶

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

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

$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
?>
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

Sự khác biệt giữa việc truy cập một biến theo tên và tham chiếu trong PHP là gì?

Trong cuộc gọi theo giá trị, một bản sao của biến được truyền trong khi trong cuộc gọi bằng tham chiếu, một biến được truyền. Trong cuộc gọi theo giá trị, các đối số thực tế và chính thức sẽ được tạo ở các vị trí bộ nhớ khác nhau trong khi trong cuộc gọi bằng tham chiếu, các đối số thực tế và chính thức sẽ được tạo ở cùng một vị trí bộ nhớ.. In Call by value, actual and formal arguments will be created in different memory locations whereas in Call by reference, actual and formal arguments will be created in the same memory location.

Sự khác biệt giữa cuộc gọi theo giá trị và cuộc gọi bằng tham chiếu trong PHP là gì?

Trong trường hợp gọi theo giá trị, khi chúng ta chuyển giá trị của tham số trong quá trình gọi hàm, nó sao chép chúng vào đối số cục bộ thực tế của hàm. Trong trường hợp gọi bằng tham chiếu, khi chúng tôi chuyển tham chiếu/địa chỉ vị trí của tham số, nó sao chép và gán chúng cho đối số cục bộ của hàm.

Sự khác biệt giữa truyền qua giá trị và thông qua tham chiếu trong PHP là gì?

Sự định nghĩa.Truyền bằng giá trị đề cập đến một cơ chế sao chép giá trị tham số chức năng sang biến khác trong khi vượt qua tham chiếu đề cập đến một cơ chế truyền các tham số thực tế cho hàm.Do đó, đây là sự khác biệt chính giữa vượt qua giá trị và vượt qua tham chiếu.

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.