Passing by Reference
Passing an array by reference allows the function to modify the original array directly.
PHP
<?php function arrayByReference(& $arr ) { // Modify the original array $arr [0] = 100; } // Driver code $arr = [1, 2, 3]; // Function call arrayByReference( $arr ); print_r( $arr ); ?> |
Output
Array ( [0] => 100 [1] => 2 [2] => 3 )
How to Pass an Array into a Function in PHP ?
This article will show you how to pass an array to function in PHP. When working with arrays, it is essential to understand how to pass them into functions for effective code organization and re-usability. This article explores various approaches to pass arrays into PHP functions, covering different methods and scenarios.
Table of Content
- Passing by Value
- Passing by Reference
- Using the func_get_args() Function
Contact Us