Hướng dẫn php function reference - tham chiếu 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:

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

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

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

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

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

foo($a)1

Jason Steelman ¶

2 năm trước

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

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

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

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

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

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

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

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

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

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

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

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

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

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 >= 5.60

// PHP >= 5.61

// PHP >= 5.62

foo($a)1

Rob tại Librobert Dot Net

7 năm trước

// PHP >= 5.64

// PHP >= 5.65

// PHP >= 5.66

foo($a)1

Nickshanks tại Nickshanks Dot Com ¶

5 năm trước

// PHP >= 5.68

// PHP >= 5.69

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