473,671 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reference passing

Hello, I have a function I use to remove all the whitespace from the
beginning and end of all the $_REQUEST variables. A $_REQUEST variable may
also be an array so I call it recursively. I have used as a formal
parameter a referenced passed variable. So any changes to the array inside
the function should also occur to the array outside the function.
The function I use is shown below...

//the array pointer must be reset to the start before this function is
called
// for the first time. It would be a safe idea to call
resset($inputVa riable) after last use as well.
function removeWhitespac eRecursively(&$ anArray){
while (list($key,$val ) = each ($anArray)){
if (is_array($val) ){
removeWhitespac eRecursively($v al);
$anArray[$key] = $val; //unknown why required.
}else{
$anArray[$key] = trim($val);

}
}
}
In testing this function. It worked well for simple $_REQUEST variables
that had no arrays in them but when an array was in the $_REQUEST array then
the line marked "//unknown why required" was found to be needed otherwise
the changes to the recursively altered array did not propagate to the
original array.
eg...
given the input...
$_REQUEST['name'] = " noel ";
$_REQUEST['phone'][0] = " 1234 ";
$_REQUEST['phone'][1] = " 5678 ";

The output without the marked line was...
$_REQUEST['name'] = "noel";
$_REQUEST['phone'][0] = " 1234 ";
$_REQUEST['phone'][1] = " 5678 ";

The output with the marked line was...
$_REQUEST['name'] = "noel";
$_REQUEST['phone'][0] = "1234";
$_REQUEST['phone'][1] = "5678";

Can anyone please explain why the changes to the recursively changed array
did not propagate to the original array?
Thanks for your help.
Jul 17 '05 #1
2 1578


Noel Wood wrote:
Hello, I have a function I use to remove all the whitespace from the
beginning and end of all the $_REQUEST variables. A $_REQUEST variable may
also be an array so I call it recursively. I have used as a formal
parameter a referenced passed variable. So any changes to the array inside
the function should also occur to the array outside the function.
The function I use is shown below...

//the array pointer must be reset to the start before this function is
called
// for the first time. It would be a safe idea to call
resset($inputVa riable) after last use as well.
function removeWhitespac eRecursively(&$ anArray){
while (list($key,$val ) = each ($anArray)){
if (is_array($val) ){
removeWhitespac eRecursively($v al);
$anArray[$key] = $val; //unknown why required.
$val - is a copy of array $anArray[$key]
you pass copy to your function, modify it
and after thar you _must_ assign it to your
target output array $anArray[$key]
}else{
$anArray[$key] = trim($val);

}
}
}
In testing this function. It worked well for simple $_REQUEST variables
that had no arrays in them but when an array was in the $_REQUEST array then
the line marked "//unknown why required" was found to be needed otherwise
the changes to the recursively altered array did not propagate to the
original array.
eg...
given the input...
$_REQUEST['name'] = " noel ";
$_REQUEST['phone'][0] = " 1234 ";
$_REQUEST['phone'][1] = " 5678 ";

The output without the marked line was...
$_REQUEST['name'] = "noel";
$_REQUEST['phone'][0] = " 1234 ";
$_REQUEST['phone'][1] = " 5678 ";

The output with the marked line was...
$_REQUEST['name'] = "noel";
$_REQUEST['phone'][0] = "1234";
$_REQUEST['phone'][1] = "5678";

Can anyone please explain why the changes to the recursively changed array
did not propagate to the original array?
Thanks for your help.

Jul 17 '05 #2
Noel Wood wrote:
function removeWhitespac eRecursively(&$ anArray){
while (list($key,$val ) = each ($anArray)){
if (is_array($val) ){
removeWhitespac eRecursively($v al);
$anArray[$key] = $val; //unknown why required.
}else{
$anArray[$key] = trim($val);

}
}
}

Can anyone please explain why the changes to the recursively changed array
did not propagate to the original array?
Thanks for your help.


Because when you get list($key,$val) out of $anArray you are making a
copy of the variable, not a reference to the variable. You can work
around this by passing $anArray[$key] to removeWhitespac eRecursively()
when calling it from within the function.

A better way to implement this function would be:

function removeWhitespac eRecursively(&$ anArray) {
foreach($anArra y as $key=>$val) {
if(is_array($va l))
removeWhitespac eRecursively($a nArray[$key]);
else
$anArray[$key] = trim($val);
}
}

--
Jasper Bryant-Greene
Cabbage Promotions
Jul 17 '05 #3

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

Similar topics

6
9270
by: Randell D. | last post by:
Folks, I've asked this before but never got any response but its important and I thought I'd pitch it again (hopefully a bit clearer)... One can pass data from one function to another either by passing a copy, or passing by reference. My understanding of passing by reference (putting the & before a variable during the function declaration) means that the original data is used.
36
28095
by: Riccardo Rossi | last post by:
Hi all! How does Python pass arguments to a function? By value or by reference? Thanks, Riccardo Rossi.
13
17916
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I create another Area and assign myArea to it (ie: Area foo = myArea;) or is there a better way? ~AF
8
2114
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects, each requiring a call to that method to fulfill their purpose. There could be 200, there could be more than 1000. That is a lot of references passed around. It feels heavy. Let us say i changed the signature of the interface method to:
12
2678
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
12
5386
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
9
2437
by: Jack | last post by:
If I don't specify "ref" in the argument list when passing an array to the callee, I am passing the array (reference) by value. But this makes me confused because it actually means a "reference" of a "reference"??As I pass the array by value, the callee can change that array. However,when I use ref, the callee and caller point to two different arrays. So could anyone please explain the 2 different situations?
13
3020
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody bother in C# with pass-by-reference using the "ref" keyword or "out" for objects in a method parameter list, when, after all, it appears in C# that for all intents and purposes a reference is always being passed, rather than the real object (with, it...
12
3010
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables. Only very few global functions are allowed to be reusability for the programmers to use. Few...
275
12254
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
8393
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,...
1
8598
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
8670
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...
0
7437
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6229
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
5696
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();...
1
2812
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1809
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.