Reference syntax

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Reference syntax

Post by EricS »

I've seen the following tow different syntaxes for passing by reference.

Code: Select all

// example 1
function foo_func(&$foo)
{
     $foo = "baz";
}
$bar = foo_func(&$biz);

// example 2
function foo_func(&$foo)
{
    $foo = "baz";
}
$bar = foo_func($biz);
The only difference between the two is that in example 2 the passing variable in the function call does not have an ampersand.

PHP.net examples use syntax like number 2. A lot of tutorials I'm reading and classes I've downloaded use syntax like number 1.

Is the syntax for number 1 being deprecated, or is this just a "You do it one way, I perfer to do it another way" type of thing.

I read on PHP.net that not including the ampersand in the function definition is being deprecated, but that doesn't address either of the two examples above.

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

$bar = foo_func(&$biz);
and you will find something like
PHP Warning: Call-time pass-by-reference has been deprecated - argument passed by value; If you would like to pass it by reference, modify the declaration of foo_func(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file. However, future versions may not support this any longer. <script> on line <n>
in your server-log ;)
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

Yes .. the second one is correct.
It corresponds the the same thing in C/C++ such as alot of php syntax
Post Reply