Explain function call by reference
Explain function call by reference
In PHP, function call by reference is a way to pass arguments to a function where the function can modify the original value of the argument. This is achieved by using the ampersand (&) symbol before the parameter name in the function definition and when calling the function.
Here's how it works:
When you pass an argument by reference, you're essentially creating an alias for the original variable inside the function. Any changes made to the argument within the function will affect the original variable outside the function.
To pass an argument by reference, you need to use the ampersand (&) symbol before the variable name when calling the function. For example:
function incrementByReference(&$value) {
$value++;
}
$number = 5;
incrementByReference($number);
echo $number; // Output: 6
In this example, the $value
parameter in the incrementByReference()
function is passed by reference. When we call the function with $number
as an argument, the function creates an alias for $number
inside the function. Incrementing $value
inside the function d...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào