Hướng dẫn is php pass by reference or pass by value? - là php truyền bằng tham chiếu hay truyền theo giá trị?

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 ¶

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

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

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

11 thàng trước

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

Jason Steelman ¶

2 năm trước

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

PHPNet tại Holodyn dot com ¶

8 năm trước

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

ccb_bc tại hotmail dot com ¶

3 năm trước

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

Mike tại Eastghost Dot Com ¶

2 năm trước

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

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

<?php
// PHP >= 5.6
0

<?php
// PHP >= 5.6
1

<?php
// PHP >= 5.6
2

foo($a)1

Rob tại Librobert Dot Net

7 năm trước

<?php
// PHP >= 5.6
4

<?php
// PHP >= 5.6
5

<?php
// PHP >= 5.6
6

foo($a)1

Nickshanks tại Nickshanks Dot Com ¶

5 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

Rob tại Librobert Dot Net

11 thàng 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.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

Jason Steelman ¶

11 thàng 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.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

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

Các mảng PHP có được truyền qua tham chiếu không?

Liên quan đến câu hỏi đầu tiên của bạn, mảng được truyền qua tham chiếu trừ khi nó được sửa đổi trong phương thức / hàm bạn đang gọi. Nếu bạn cố gắng sửa đổi mảng trong phương thức / hàm, một bản sao của nó được tạo trước tiên và sau đó chỉ có bản sao được sửa đổi.the array is passed by reference UNLESS it is modified within the method / function you're calling. If you attempt to modify the array within the method / function, a copy of it is made first, and then only the copy is modified.

Mã PHP được thông qua như thế nào?

Trong PHP, các đối số cho một hàm có thể được truyền bằng giá trị hoặc được truyền bằng tham chiếu.Theo mặc định, các giá trị của các đối số thực tế được truyền bởi giá trị cho các đối số chính thức trở thành các biến cục bộ bên trong hàm.Do đó, việc sửa đổi các biến này không thay đổi giá trị của biến đối số thực tế.by value or passed by reference. By default, values of actual arguments are passed by value to formal arguments which become local variables inside the function. Hence, modification to these variables doesn't change value of actual argument variable.

Truyền qua giá trị hay vượt qua tham chiếu?

Pass by giá trị: Các giá trị tham số phương thức được sao chép vào một biến khác và sau đó đối tượng được sao chép được truyền, đó là lý do tại sao nó được gọi là Pass by giá trị.Truyền qua tham chiếu: Một bí danh hoặc tham chiếu đến tham số thực tế được chuyển đến phương thức, đó là lý do tại sao nó được gọi là Pass by tham chiếu.: The method parameter values are copied to another variable and then the copied object is passed, that's why it's called pass by value. Pass by Reference: An alias or reference to the actual parameter is passed to the method, that's why it's called pass by reference.

Có đi qua bằng tham chiếu PHP nhanh hơn không?

Trong thực tế, trong hầu hết các kịch bản đi qua giá trị là nhanh hơn và ít bộ nhớ hơn so với truyền qua tham chiếu.Công cụ Zend, lõi của PHP, sử dụng cơ chế tối ưu hóa sao chép không có chữ viết, không tạo ra một bản sao của một biến cho đến khi nó được sửa đổi.in most scenarios passing by value is faster and less memory intensive than passing by reference. The Zend Engine, PHP's core, uses a copy-on-write optimization mechanism that does not create a copy of a variable until it is modified.