References and Functions

As a default, functions receive arguments on a call-by-value basis. This means that a function receives the value of a variable, not the actual variable itself. To alter the value of a variable within a function, you need to either use the global statement or pass the variable by reference.

Here's an example of how functions normally operate:

function change_value ($variable) { 
   $variable++;
   echo $variable;
}
$variable = 1;
change_value ($variable);

The echo statement will print a 2, but the value of $variable is still 1 (remember that $variable within the function is not the same as $variable outside of it, despite the common name). However, if you were to pass $variable to the function as a reference, the value of ...

Get PHP Advanced for the World Wide Web: Visual QuickPro Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.