PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > assigned values in 'ByRef' call
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
assigned values in 'ByRef' call

Réponse
 
LinkBack Outils de la discussion
Vieux 06/11/2007, 20h27   #1
Csaba Gabor
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut assigned values in 'ByRef' call

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 <br>\n";

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

  Réponse avec citation
Vieux 06/11/2007, 21h02   #2
Darko
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigned values in 'ByRef' call

On Nov 6, 8:27 pm, Csaba Gabor <dans...@gmail.com> 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 <br>\n";
>
> function test(&$val) {
> print "val pre: $val <br>\n";
> $val = "else";
> print "val post: $val <br>\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.

  Réponse avec citation
Vieux 06/11/2007, 21h06   #3
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigned values in 'ByRef' call

On Nov 6, 2:27 pm, Csaba Gabor <dans...@gmail.com> 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 <br>\n";
>
> function test(&$val) {
> print "val pre: $val <br>\n";
> $val = "else";
> print "val post: $val <br>\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.

  Réponse avec citation
Vieux 06/11/2007, 21h09   #4
Darko
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigned values in 'ByRef' call

On Nov 6, 9:06 pm, ZeldorBlat <zeldorb...@gmail.com> wrote:
> On Nov 6, 2:27 pm, Csaba Gabor <dans...@gmail.com> 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 <br>\n";

>
> > function test(&$val) {
> > print "val pre: $val <br>\n";
> > $val = "else";
> > print "val post: $val <br>\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.

  Réponse avec citation
Vieux 06/11/2007, 21h15   #5
Csaba Gabor
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigned values in 'ByRef' call

On Nov 6, 9:09 pm, Darko <darko.maksimo...@gmail.com> wrote:
> On Nov 6, 9:06 pm, ZeldorBlat <zeldorb...@gmail.com> wrote:
>
> > On Nov 6, 2:27 pm, Csaba Gabor <dans...@gmail.com> 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 <br>\n";

>
> > > function test(&$val) {
> > > print "val pre: $val <br>\n";
> > > $val = "else";
> > > print "val post: $val <br>\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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 16h51.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12722 seconds with 13 queries