assigned values in "ByRef" call

assigned values in "ByRef" call

am 06.11.2007 20:27:30 von Csaba Gabor

In the following PHP code, the final printed line shows 'frob:
something'. Why is it not 'frob: else'? After all, if I replace the
first line with $frob = "something"; test ($frob); then the final
printed line does show 'frob: else'

Csaba Gabor from Vienna
PHP 5.2.4 on WinXP Pro

test ($frob = "something");
print "frob: $frob
\n";

function test(&$val) {
print "val pre: $val
\n";
$val = "else";
print "val post: $val
\n"; }

Re: assigned values in "ByRef" call

am 06.11.2007 21:02:11 von darko

On Nov 6, 8:27 pm, Csaba Gabor wrote:
> In the following PHP code, the final printed line shows 'frob:
> something'. Why is it not 'frob: else'? After all, if I replace the
> first line with $frob = "something"; test ($frob); then the final
> printed line does show 'frob: else'
>
> Csaba Gabor from Vienna
> PHP 5.2.4 on WinXP Pro
>
> test ($frob = "something");
> print "frob: $frob
\n";
>
> function test(&$val) {
> print "val pre: $val
\n";
> $val = "else";
> print "val post: $val
\n"; }

Interesting, indeed. In C++, for an example, it must work as you
expected, and works indeed, since "something" is first assigned to
$frob, then it's passed over to the function, and it does whatever it
wants with it. Here, however, some double assignments seem to have
happened. Is it a bug, or is it some kind of php-specific behaviour,
other users might now.

Re: assigned values in "ByRef" call

am 06.11.2007 21:06:15 von zeldorblat

On Nov 6, 2:27 pm, Csaba Gabor wrote:
> In the following PHP code, the final printed line shows 'frob:
> something'. Why is it not 'frob: else'? After all, if I replace the
> first line with $frob = "something"; test ($frob); then the final
> printed line does show 'frob: else'
>
> Csaba Gabor from Vienna
> PHP 5.2.4 on WinXP Pro
>
> test ($frob = "something");
> print "frob: $frob
\n";
>
> function test(&$val) {
> print "val pre: $val
\n";
> $val = "else";
> print "val post: $val
\n"; }

It's because you're not really passing a variable -- you're passing
the result of an expression. If you enable E_STRICT you'll get the
following:

Strict Standards: Only variables should be passed by reference

Since you haven't passed a variable, modifying the value inside the
function is only modifying the local value.

Re: assigned values in "ByRef" call

am 06.11.2007 21:09:49 von darko

On Nov 6, 9:06 pm, ZeldorBlat wrote:
> On Nov 6, 2:27 pm, Csaba Gabor wrote:
>
> > In the following PHP code, the final printed line shows 'frob:
> > something'. Why is it not 'frob: else'? After all, if I replace the
> > first line with $frob = "something"; test ($frob); then the final
> > printed line does show 'frob: else'
>
> > Csaba Gabor from Vienna
> > PHP 5.2.4 on WinXP Pro
>
> > test ($frob = "something");
> > print "frob: $frob
\n";
>
> > function test(&$val) {
> > print "val pre: $val
\n";
> > $val = "else";
> > print "val post: $val
\n"; }
>
> It's because you're not really passing a variable -- you're passing
> the result of an expression. If you enable E_STRICT you'll get the
> following:
>
> Strict Standards: Only variables should be passed by reference
>
> Since you haven't passed a variable, modifying the value inside the
> function is only modifying the local value.

Yes, I've thought about it and was just coming back to brag about the
conclusion, but you were faster. In C++, this is not the behavior, but
the assignment operator returns the reference to the left parameter
instead, making it possible. In PHP, however, only the value (copy of
the value, if you like) is returned from the assignment operator,
which passes that as the argument, not the $frob variable.

Re: assigned values in "ByRef" call

am 06.11.2007 21:15:45 von Csaba Gabor

On Nov 6, 9:09 pm, Darko wrote:
> On Nov 6, 9:06 pm, ZeldorBlat wrote:
>
> > On Nov 6, 2:27 pm, Csaba Gabor wrote:
>
> > > In the following PHP code, the final printed line shows 'frob:
> > > something'. Why is it not 'frob: else'? After all, if I replace the
> > > first line with $frob = "something"; test ($frob); then the final
> > > printed line does show 'frob: else'
>
> > > Csaba Gabor from Vienna
> > > PHP 5.2.4 on WinXP Pro
>
> > > test ($frob = "something");
> > > print "frob: $frob
\n";
>
> > > function test(&$val) {
> > > print "val pre: $val
\n";
> > > $val = "else";
> > > print "val post: $val
\n"; }
>
> > It's because you're not really passing a variable -- you're passing
> > the result of an expression. If you enable E_STRICT you'll get the
> > following:
>
> > Strict Standards: Only variables should be passed by reference
>
> > Since you haven't passed a variable, modifying the value inside the
> > function is only modifying the local value.
>
> Yes, I've thought about it and was just coming back to brag about the
> conclusion, but you were faster. In C++, this is not the behavior, but
> the assignment operator returns the reference to the left parameter
> instead, making it possible. In PHP, however, only the value (copy of
> the value, if you like) is returned from the assignment operator,
> which passes that as the argument, not the $frob variable.

Thanks to both of you for a very nice explanation,
Csaba Gabor from Vienna