
Originally Posted by
djr33
Can you, as in your example, create a variable at the same time as using it by reference in a function? Or does it need to already exist in the current scope before you do that?
Yes; works just fine. No errors, warnings, nothing.
Also, this should work everywhere (although I never mess around with PHP 4 anymore). Sometime - early in PHP 5, *I think* - something called "call time pass by reference" was deprecated and stopped working. That's when you use &
with the argument (or return value) instead of in the function definition:
PHP Code:
some_function( $a ){ return $a; }
$arg = 'value';
// bad! doesn't work
some_function( &$arg );
// bad! doesn't work
$return =& some_function( $arg );
reference_function( &$a ){ return &$a; }
// $arg passed in by reference
reference_function( $arg );
// return value assigned by reference
// this is actually *exactly* the same as doing $return = 'value'; but wastes much more time doing it
$return = reference_function( 'value' );
Basically, in recent versions of PHP, you can define functions that accept/return by reference, but you can't force a function to accept/return by reference if it wasn't designed to.
Bookmarks