RSS
 

Pass by Reference in PHP5

14 Aug

PHP5 has a Pass-by-reference notation: &$bar in line 3 as seen here in the parameter to a function bar(); Basically, this is just a point from the sub routine’s “$bar” back to the caller’s $foo. In this case, a change to the reference variable $bar is reflected in the main variable $foo.

“bar points to/references foo”
$bar => $foo;


1

Output:


foo:foo
bar:foo
bar:bar
foo:bar

Recommended reading: PHP 5 Objects, Patterns, and Practice

GD Star Rating
a WordPress rating system
GD Star Rating
a WordPress rating system
Pass by Reference in PHP5, 8.0 out of 10 based on 1 rating
Retweet
 
  1. reader

    July 30, 2008 at 7:55 am

    I’d like to pass by value…

    GD Star Rating
    a WordPress rating system
    GD Star Rating
    a WordPress rating system
     
  2. Ian

    November 12, 2008 at 7:16 am

    Just leave out the ampersand to pass by value, eg.

    function bar($bar = NULL)

    GD Star Rating
    a WordPress rating system
    GD Star Rating
    a WordPress rating system