Page 1 of 1

Reference syntax

Posted: Thu Mar 27, 2003 2:09 pm
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

Posted: Thu Mar 27, 2003 4:52 pm
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 ;)

Posted: Thu Mar 27, 2003 4:58 pm
by lazy_yogi
Yes .. the second one is correct.
It corresponds the the same thing in C/C++ such as alot of php syntax