Re: return a reference but it is a number, like 100

Re: return a reference but it is a number, like 100

am 02.10.2007 03:24:16 von Michael Fesser

..oO(Summercool)

>the following code will let foo() return a reference something, but
>the thing being returned is a number. I thought there is no "alias"
>to a number, only to a variable?

Correct. Your code throws a notice "Only variable references should be
returned by reference [...]" since PHP 4.4.0 and 5.1.0.

>Is returning of a reference mainly
>for returning a big array or for returning an object in PHP4?

From the manual:

| Do not use return-by-reference to increase performance, the engine is
| smart enough to optimize this on its own. Only return references when
| you have a valid technical reason to do it!

> function &foo(&$i) {
> $i = 10;
> return 100;
> }
>
> $j = 1;
> print_r("$j\n");
>
> $k = foo($j);

If you return by reference, you have to use the '&' twice - for
returning the reference from the function and for assigning it to the
variable:

$k = &foo($j);

Micha