473,832 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass array names to function parameters ?


i'm trying to itterate through an array that contains the names of the
global arrays

eg:

$myarray = array("\$_GET", "\$_SERVER" ); and so on

The problem Im having is calling a function with

myfunction($mya rray[0]);

doesnt seem to pass the array name in a manner I can then use.

I'm sure I'm missing something simple here can anyonehelp please ?

tony
Apr 15 '06 #1
10 15445
On Sat, 2006-04-15 at 12:26 +0000, to**@tony.com wrote:
i'm trying to itterate through an array that contains the names of the
global arrays

eg:

$myarray = array("\$_GET", "\$_SERVER" ); and so on

The problem Im having is calling a function with

myfunction($mya rray[0]);

doesnt seem to pass the array name in a manner I can then use.

I'm sure I'm missing something simple here can anyonehelp please ?

tony


That should pass the name of the global array. You could also use single
quotes and drop the escape character:

$myarray = array('$_GET, '$_SERVER');

Are you sure the problem isn't in your function? What are you doing
with the name once your function receives it?

Scott
Apr 15 '06 #2
Actually, you cannot use superglobal arrays as variable variables:
From PHP manual:

http://www.php.net/manual/en/languag...predefined.php

"Variable variables: Superglobals cannot be used as variable variables
inside functions or class methods."

Apr 15 '06 #3
In article <11************ **********@v46g 2000cwv.googleg roups.com>,
da****@gmail.co m says...
Actually, you cannot use superglobal arrays as variable variables:
From PHP manual:

http://www.php.net/manual/en/languag...predefined.php

"Variable variables: Superglobals cannot be used as variable variables
inside functions or class methods."


Oh bugger.

Thanks for that.

I was just trying to get all the superglobals to echo out to the browser
without having to code every one seperately...
something like:

function showthisarray($ nextGlobal) {
echo($nextGloba l . "\n"); // debug line
foreach($nextGl obal as $k => $v ) {
echo($k . " = " . $v . "\n");
}
}

The idea being to just pass the array names in sequence or as selected.

The function does get the name of the relevant array as the first echo
prints it ok eg . $_GET or whatever as is passed into $nextGlobal

But the foreach() function just generates a bad parameter error.

I guess it will have to be the long winded solution.

tony
Apr 15 '06 #4
But you still can try with $HTTP_*_VARS :)

// $nextGlobal = {"GET", "POST", ...}

function showthisarray($ nextGlobal) {
echo($nextGloba l . "\n"); // debug line
foreach(${'HTTP _' . $nextGlobal . '_VARS'} as $k => $v ) {
echo($k . " = " . $v . "\n");
}
}

I didn't write much PHP lately, so sorry if there is some syntax (or
any other) error here.

Apr 15 '06 #5
In article <11************ **********@j33g 2000cwa.googleg roups.com>,
da****@gmail.co m says...
But you still can try with $HTTP_*_VARS :)

// $nextGlobal = {"GET", "POST", ...}

function showthisarray($ nextGlobal) {
echo($nextGloba l . "\n"); // debug line
foreach(${'HTTP _' . $nextGlobal . '_VARS'} as $k => $v ) {
echo($k . " = " . $v . "\n");
}
}

I didn't write much PHP lately, so sorry if there is some syntax (or
any other) error here.


Worth a try but it produces the same result. It seems you can't pass the
array name in a string at all from what I can see.

tony
Apr 16 '06 #6
I knew that I forgot something :/

$HTTP_*_VARS arrays are not superglobal. Hence you need to specify in
function that you're using global arrays or access them with $GLOBALS
array

Here's a bit modified example that should work:

------------------------

// custom global array
$myArray = array("some", "elements") ;

// array with names of global arrays you want to access
$globalArrayNam es = array("HTTP_GET _VARS", "HTTP_POST_VARS ",
"myArray");

// parameter is global array name
function showThisArray($ globalArrayName ) {
echo($globalArr ayName . "\n"); // debug line
foreach($GLOBAL S[$globalArrayNam e] as $k => $v ) {
echo($k . " = " . $v . "\n");
}
}

showThisArray($ globalArrayName s[0]);
showThisArray($ globalArrayName s[1]);
showThisArray($ globalArrayName s[2]);

------------------------

Hope this helps

Apr 16 '06 #7
In article <11************ **********@i40g 2000cwc.googleg roups.com>,
da****@gmail.co m says...
I knew that I forgot something :/


Nice one. Modified slightly & tested - it works fine - as below.
I added SERVER just to be sure of some output from a Sglobal
So much nicer than having to do each one at a time ;-)

<?php
// custom global array
$myArray = array("some", "elements") ;

// array with names of global arrays you want to access
$globalArrayNam es = array("HTTP_SER VER_VARS", "HTTP_POST_VARS ",
"myArray");

// parameter is global array name
function showThisArray($ globalArrayName ) {
echo("<br><br>" . "[ " . $globalArrayNam e . " ]<br>");
foreach($GLOBAL S[$globalArrayNam e] as $k => $v ) {
echo($k . " = " . $v . "<br>");
}
}

showThisArray($ globalArrayName s[0]);
showThisArray($ globalArrayName s[1]);
showThisArray($ globalArrayName s[2]);
?>

tony
Apr 17 '06 #8

to**@tony.com wrote:
In article <11************ **********@i40g 2000cwc.googleg roups.com>,
da****@gmail.co m says...
I knew that I forgot something :/


Nice one. Modified slightly & tested - it works fine - as below.
I added SERVER just to be sure of some output from a Sglobal
So much nicer than having to do each one at a time ;-)


Here's another way of doing this.

<?php
// custom global array
$myArray = array("some", "elements") ;

// array with names of global arrays you want to access
$globalArrayNam es =
array(array('$_ SERVER',$_SERVE R),array('$_POS T',$_POST),
array("myArray" ,$myArray));

// parameter is global array name
function showThisArray($ globalArrayName ) {
echo("<br><br>" . "[ " . $globalArrayNam e[0] . " ]<br>");
echo '<pre>' . print_r($global ArrayName[1],true) . '</pre>';
}

showThisArray($ globalArrayName s[0]);
showThisArray($ globalArrayName s[1]);
showThisArray($ globalArrayName s[2]);
?>
Ken

Apr 17 '06 #9
In article <11************ **********@g10g 2000cwb.googleg roups.com>,
ke******@gmail. com says...
Here's another way of doing this.

<?php
// custom global array
$myArray = array("some", "elements") ;

// array with names of global arrays you want to access
$globalArrayNam es =
array(array('$_ SERVER',$_SERVE R),array('$_POS T',$_POST),
array("myArray" ,$myArray));

// parameter is global array name
function showThisArray($ globalArrayName ) {
echo("<br><br>" . "[ " . $globalArrayNam e[0] . " ]<br>");
echo '<pre>' . print_r($global ArrayName[1],true) . '</pre>';
}

showThisArray($ globalArrayName s[0]);
showThisArray($ globalArrayName s[1]);
showThisArray($ globalArrayName s[2]);
?>
Ken


And there was me thinking it couldnt be done ... ;-)
Now we have two ways.

Correct me if I'm wrong though Ken but doesnt this method pass the actual
array rather than just the name?

I'm not sure because I'm not able to work out what this is doing exactly
(I'm new to PHP as you may have gathered)

tony

Apr 18 '06 #10

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

Similar topics

1
5955
by: Miguel | last post by:
Hello. I have a Javascript function that validates an specific form. As parameters this function receives an array of elements to be checked. Depending on the form in cause, the array could have variable lenght. I don't know how can i pass a dynamic array on 'onsubmit' event form PHP code to a JavaScript function. I would like to do something like: <script>
5
3431
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? /******************************************************** Below is my code: ********************************************************/
9
2320
by: Alan Silver | last post by:
Hello, I'm a bit surprised at the amount of boilerplate code required to do standard data access in .NET and was looking for a way to improve matters. In Classic ASP, I used to have a common function that was included in all pages that took an SQL query and returned a disconnected recordset. This meant that data access could be achieved in a single line. I would like to do something similar in ASP.NET. I know I could just duplicate...
3
2835
by: questions? | last post by:
I tried to pass a two dimensional array in the function arguments the following program is a demonstration, ******************************************** # include <stdio.h> # include <string.h> double data={{1.0, 3.0},{9.0, 8.0}};
10
1572
by: tshad | last post by:
I want to access multiple arguments based on name passed. For example I have the following asp:textboxes: BillingAddress1 BillingAddress2 BillingCity ShippingAddress1 ShippingAddress2 ShippingCity
14
20417
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case the value if modified in the function will get reflected in the main function as well. I dont...
28
4721
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
10
13670
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the function is also modified. Sometimes I wish to work with "copies", in that when I pass in an integer variable into a function, I want the function to be modifying a COPY, not the reference. Is this possible?
12
11121
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
9795
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
10498
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...
0
10212
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
9319
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
7753
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
6951
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();...
0
5623
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3077
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.