473,800 Members | 2,726 Online
Bytes | Software Development & Data Engineering Community
+ 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 2024
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...@gma il.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.comwro te:
On Nov 6, 9:06 pm, ZeldorBlat <zeldorb...@gma il.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
43961
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 will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. */ Technet Script Center - http://www.microsoft.com/technet/scriptcenter/ WSH 5.6 Documentation -...
10
4449
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 variables and arrays are always passed by value and objects (I tried dictionary ones) are always passed by reference. Is it correct? Or I'm wrong and I did a mistake on my test code (that you can find below)? Can someone explain to me why there...
16
17413
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
12
6908
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 Array Variable values are calculated in the function. Calling procedure receives the values preferably into variables of the same
6
1697
by: VJ | last post by:
Does ByRef work when calling a Method in DLL from a Executable? VJ
5
4226
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 company, much of my coworker works with Public sub ( byval Itemx as integer, byref returnCode as Integer). I don't understand why they use this kind of sub and i'm wondering if this kind of sub take more time to response?
23
1052
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 a number. How is this possible? Thanks -- I'll have a B please Bob.
2
2333
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 : the class Id of objects owning attributes to
4
1359
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 As System.EventArgs) Handles Button3.Click Dim a As Integer, b As Integer, c As Integer = 1 Dim d As Boolean = False MsgBox(a & " " & b & " " & c) d = Testit(a, b, c) MsgBox(a & " " & b & " " & C)
0
9691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10507
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10279
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10255
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10036
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7582
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6815
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3765
muto222
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.