Skip to content
PHP Logo

How does PHP handle reference data types and copying of arrays when passed to functions?

When passing arrays to functions in PHP, they are typically passed by value, meaning a copy of the array is made. However, if you want to pass an array by reference to a function and have the function modify the original array, you can use the `&` symbol in the function parameter declaration.

php
// Passing array by reference to a function
function modifyArray(&$arr) {
    $arr[0] = "modified";
}

$myArray = ["original", "value"];
modifyArray($myArray);

print_r($myArray); // Output: Array ( [0] => modified [1] => value )