PHP Variables passed by value or by reference?
I had a Java developer new to PHP ask me this question the other day, so I thought I'd document the answer here. Question: are PHP variables passed by value or by reference?...
I had a Java developer new to PHP ask me this question the other day, so I thought I'd document the answer here. Question: are PHP variables passed by value or by reference?...
When learning a new programming language, one of the possible roadblocks you might encounter is the question whether the language is, by default, pass-by-value or pass-by-reference So here is my question to all of you, in your favorite language, how is it actually done? and what are the possible pitfalls? your favorite language can, of...
I always thought Java was pass by reference, however I've seen a couple of blog posts (e.g. this blog) that claim it's not. I don't think I understand the distinction they're making. Could someone explain it please? ...
Is it better in C++ to pass by value or pass by constant reference? I am wondering which is better practice. I realize that pass by constant reference should provide for better performance in the program because you are not making a copy of the variable. ...
If I have a function that needs to work with a shared_ptr, wouldn't it be more efficient to pass it a reference to it (so to avoid copying the shared_ptr object)? What are the possible bad side effects? I envision two possible cases: 1) inside the function a copy is made of the argument, like in ClassA::take_copy_of_sp(boost::shared_pt...
Hi, Somewhat unclear to me are references (pointers?) to classes in Visual Basic .Net. The question I am about to ask can be answered by a little bit of testing, but I was wondering if anybody could post a decent explanation (or links, too). If you create a class: Public Class ReferenceClass private myBooleanValue as Boolean = F...
I'm new to C, and am confused by results I'm getting when referencing a member of a struct via a pointer. See the following code for an example. What's happening when I reference tst->number the first time? What fundamental thing am I missing here? #include <stdio.h> #include <stdlib.h> typedef struct { int number...
Hi, I would like to clarify the differences between by value and by reference. I drew a picture So, for passing by value, a copy of an identical object is created with a different reference, and the local variable is assigned the new reference, so to point to the new copy How to understand the words: " If the function modifies tha...
Hello, I have a problem with understanding the "pass-by-value" action of Java in the following example: public class Numbers { static int[] s_ccc = {7}; static int[] t_ccc = {7}; public static void calculate(int[] b, int[] c) { System.out.println("s_ccc[0] = " + s_ccc[0]); // 7 System.out.println("t_ccc[0] = " ...
I'm fairly new to Java (been writing other stuff for many years) and unless I'm missing something (and I'm happy to be wrong here) the following is a fatal flaw... String foo = new String(); thisDoesntWork(foo); System.out.println(foo);//this prints nothing public static void thisDoesntWork(String foo){ foo = "howdy"; } Now, I'm w...
When you pass a collection like list, array to another function in python, does it make a copy of it, or is it just a pointer? ...
I'm trying to pass an object out of a function. Here's my code: <script type="text/javascript"> // finds the head element; creates a script with passed url; appends it to the head function loadJSON(url) { var headID = document.getElementsByTagName("head")[0]; var newScript = document.createElement('script'); newScrip...
So, I have some code, kind of like the following, to add a struct to a list of structs: void barPush(BarList * list,Bar * bar) { // if there is no move to add, then we are done if (bar == NULL) return;//EMPTY_LIST; // allocate space for the new node BarList * newNode = malloc(sizeof(BarList)); // assign the right v...
I found an unusual Java method today: private void addShortenedName(ArrayList<String> voiceSetList, String vsName) { if (null == vsName) vsName = ""; else vsName = vsName.trim(); String shortenedVoiceSetName = vsName.substring(0, Math.min(8, vsName.length())); //SCR10638 - Prevent export of empty rows. ...
I would like to emulate the pass-by-value behaviour in python. In other words, I would like to make absolutely sure that the function I write do not modify user supplied data. One possible way is to use deep copy: from copy import deepcopy def f(data): data = deepcopy(data) #do stuff is there more efficient or more pythonic ...
I do scientific programming, and often want to show users prompts and variable pairs, let them edit the variables, and then do the calulations with the new variables. I do this so often, that I wrote a wxPython class to move this code out of the main program. You set up a list for each variable with the type of the variable (string, fl...
is PHP and C++ the only 2 places that we need to careful about passing a simple data type variable as a function argument and the value can be changed? such as $count = 2; foo($count); echo $count; and the 3rd line, echo $count display something other than 2. I only know of PHP and C++ where it can happen. Is there any other place ...
I understand that if I pass a value-type (int, struct etc...) as a parameter (without the ref keyword), a copy of that variable is passed to the method, but if I use the ref keyword a reference to that variable is passed, not a new one. But with reference-types, like classes, even without the ref keyword a reference is passed to the met...
In the following code, I get a compile time error because i is treated as a variant. The error is: "ByRef Argument type mismatch.". But if I pass the parameters ByVal, there is no error why? Private Sub Command2_Click() Dim i, j As Integer i = 5 j = 7 Call Swap(i, j) End Sub Public Sub Swap(ByRef X As Integer, ByRef Y...
The Objective I want to dynamically assign event handlers to some divs on pages throughout a site. My Method Im using jQuery to bind anonymous functions as handlers for selected div events. The Problem The code iterates an array of div names and associated urls. The div name is used to set the binding target i.e. attach this event ...