473,472 Members | 2,153 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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"; }

Nov 6 '07 #1
4 2013
On Nov 6, 8:27 pm, Csaba Gabor <dans...@gmail.comwrote:
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.

Nov 6 '07 #2
On Nov 6, 2:27 pm, Csaba Gabor <dans...@gmail.comwrote:
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.

Nov 6 '07 #3
On Nov 6, 9:06 pm, ZeldorBlat <zeldorb...@gmail.comwrote:
On Nov 6, 2:27 pm, Csaba Gabor <dans...@gmail.comwrote:
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.

Nov 6 '07 #4
On Nov 6, 9:09 pm, Darko <darko.maksimo...@gmail.comwrote:
On Nov 6, 9:06 pm, ZeldorBlat <zeldorb...@gmail.comwrote:
On Nov 6, 2:27 pm, Csaba Gabor <dans...@gmail.comwrote:
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

Nov 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
10
by: Logico | last post by:
Hi everybody, I've tried to use the byref keyword for passing arguments to subroutines and functions in my ASP pages with VBScript, but it seems that both byref and byval are irrilevant, as simple...
16
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
12
by: Dennis D. | last post by:
Hello: I want a function to return three variables to the calling procedure: Private Function CalcTimes(ByVal iAddDays as Integer, ByVal iAddHours as Integer, ByVal iAddMins as Integer) As...
6
by: VJ | last post by:
Does ByRef work when calling a Method in DLL from a Executable? VJ
5
by: Ghislain Tanguay | last post by:
Hi! I'm maybe from old school but, for me, whebn it's time to call a method from DCOM, remoting or web service returning a string or anything else I use a function. It's more verbose. In my new...
23
by: Bob Hollness | last post by:
Hi all. I know this is basic but it is something that i have never learnt! I have a function that returns a True or False to the Sub. But what i actually want to do is return True or False and...
2
by: cantankerousoldgit | last post by:
I am trying to call a DLL with a function like this: """DESCRIPTION: Get a list of objects attributes matching attribute values ARGUMENTS: session : the current session classId :...
4
by: Jim | last post by:
I there a way to return values for multiple variable from a fuction. The following does not work because it give me a syntax error Private Sub Button3_Click(ByVal sender As System.Object, ByVal e...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.