Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Passing By Value

When calling a method, or returning from it, data may be passed by value or by reference.

By value, a copy of the data is passed: any modification on that value inside the method will not be reflected in the original value.

This is the default way of passing data to methods: the alternative way is to use by reference.

Passing by value may be memory-intensive when passing a large array. This is not the case for objects, which are always passed by reference.

Returned values are passed back to the calling context with the same rule as for parameters.

<?php

    $trunk = 0;
    trumpet($trunk);  // displays 1
    print $trunk; // displays 0
    
    function trumpet($trunk) {
        ++$trunk;
        echo $trunk;
    }

?>

Documentation

See Also