The arguments that the function receives are copies of the values that the user provided.
If you modify these arguments inside the function, it will not affect the original values.
This feature is known as sending arguments by value.
<?php function modify($a) { $a = 3; //www . ja v a 2 s .com } $a = 2; modify($a); var_dump($a); // prints 2 ?>
Here, ee declared a variable $a with value 2, and then calling the modify method sending that $a.
The modify method modifies the argument $a, setting its value to 3, but this does not affect the original value of $a, which remains 2.
To change the value of the original variable, pass the argument by reference.
To do that, you add an ampersand (&) before the argument when declaring the function:
<?php function modify(&$a) { $a = 3; // www . ja v a2 s.c o m } $a = 2; modify($a); var_dump($a); // prints 2 ?>
Now, on invoking the function modify, $a will be 3.