PHP: Parameter Passing
Derek Bridge
Department of Computer Science,
University College Cork
PHP: Parameter Passing
Aims:
- to learn what is meant by pass-by-value and pass-by-reference
- to learn when to use each of these
Common error
- Class exercise: What is the output of the
following?
function increase_prices( $product_prices, $increase )
{
foreach($product_prices as $product => $price)
{
$product_prices[$product] = $price * (1 + $increase);
}
}
$fruit_prices = array('Apples' => 1.59, 'Pears' => 2.34,
'Kumquats' => 4.05, 'Jujubes' => 2.34);
increase_prices($fruit_prices, 0.1);
echo "<ul>";
foreach($fruit_prices as $fruit => $price)
{
echo "<li>{$fruit}: {$price}</li>";
}
echo "</ul>";
Simpler example of the same error
Pass-by-value
- As we know, when a function is invoked, temporary variables
are created for each formal parameter
- By default, copies of the values of the actual
parameters are
placed into these temporary variables in order to initialise
them
- This is known as pass-by-value (or sometimes
'call-by-value')
Pass-by-value
- It follows that in pass-by-value any changes a function makes to its formal
parameters have no effect on the corresponding actual parameters
(because the function is working on copies!)
- Now we can understand why the two erroneous programs from
earlier do not behave as the programmer intended
- In the lecture, make sure you understand this fully
- Class exercise: Using only what we've
covered so far, fix the two erroneous programs
Pass-by-reference
- In PHP, when actual parameters are variables it is possible for the
programmer to request that their addresses, rather than
their values, are used to initialise the formal parameters
- This is known as pass-by-reference (or sometimes
'call-by-reference')
- You request this in the function's parameter list by prefixing an ampersand
(&) on the formal parameter
Pass-by-reference
- It follows that in pass-by-reference any changes a
function makes to its formal
parameters do effect the corresponding actual parameters
(because the addresses 'point' to the originals)
- In the lecture, make sure you understand this fully
- Class exercise: Use pass-by-reference
parameters to fix the other erroneous program
When to use pass-by-reference
- Advice: use sparingly
- When your function needs direct access to a variable in order
to modify its value
- this is a classic case to use pass-by-reference
- but we saw an alternative: return the modified value
- When your actual parameter is a very long string or very large array
- using pass-by-reference saves time and memory
- if we use pass-by-value, the whole string/array would need to be
copied into the formal parameter
Observation
- In some languages, such as PHP, the programmer chooses between pass-by-value
(default) and pass-by-reference (e.g. by using &)
- In a language such as Java, the decision is taken out of the programmers
hands:
- simple values (integers, floats, Booleans,...) are all pass-by-value
- strings, arrays (and other objects) are all, in some sense,
pass-by-reference (without using &)
Pointers for advanced students
- By default in PHP, return values are copied out of a function
- By prefixing the function name with &, the address of the value
is returned
- This facility might be useful if the return value is a long string or large array
- But in fact PHP has certain optimisations ('copy-on-write') that mean that this
facility is rarely necessary