Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › PHP: Variable name as argument in function
New Posts  All Forums:Forum Nav:

PHP: Variable name as argument in function

post #1 of 35
Thread Starter 
This may seem a bit weird, but what I'm trying to do here is to define a function. Inside this function it fetches some data from a table (defined by the first argument in the function call, let's call it foo.) in an SQL database.
After fetching the value from the database, that value now has to be stored in its own variable, and I'd like to have it so that I can call the function(foo, bar) which stores the value from the database into the $bar variable. That enables me to do something along these lines:

fetch_stat(field_12, orange);
fetch_stat(field_13, tennis);
fetch_stat(field_17, whatever);

which would yield the variables $orange, $tennis and $whatever, for example.

Is this possible? I really hope it is, because I'm looking for a way to do it without having to put all the code into the script several times, as it makes the file about 40x as big as when I could compact it into a function like this.

Thanks in advance!
Edited by RevZ - 5/3/11 at 4:58am
post #2 of 35
Yes.

Code:

$fruit = 'orange';
$$fruit = 'delicious';
echo $orange;
// outputs 'delicious' 
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
post #3 of 35
Thread Starter 
I don't want a variable variable; that's what everything I found on the entire internet was about. I want a variable that is predetermined by what I throw into the function; it has to be preset as an argument in the function call.

Let's say there's a field_10 in the database that contains the number 3 as a value, and a field_11 that holds the value 21.

I want one function to be able to just say "give me the value for this field and put it in that variable". So, in essence, for the following:

Code:

$variable = mysql_query("SELECT field_11 FROM dt_fields WHERE member_id='" . $userno . "'")

or die(mysql_error()); 
I want $variable to be whatever I put as my second argument in the function(foo,bar) and in this case it'd be fetch_stat(field_11, variable).
post #4 of 35
So you want to know how to write function calls?

Code:

function foo($bar) {
    echo "I have a $bar and I like it!";

Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
post #5 of 35
Thread Starter 
*facepalm* ugh.. No offence, but did you even read what I've explained not once but twice now? I'm trying to manipulate the name of a variable that gets created inside the function itself, by defining said name in the function call as an argument.
You can't do that the same way as you're describing, since what I need here is to define a name for the variable, not the variable itself. The variable gets created during the function, as you can see in the line of code above. That variable needs to be created with a name defined in the second argument of said function. That's all I need.
post #6 of 35
Quote:
Originally Posted by RevZ View Post
*facepalm* ugh.. No offence, but did you even read what I've explained not once but twice now? I'm trying to manipulate the name of a variable that gets created inside the function itself, by defining said name in the function call as an argument.
You can't do that the same way as you're describing, since what I need here is to define a name for the variable, not the variable itself. The variable gets created during the function, as you can see in the line of code above. That variable needs to be created with a name defined in the second argument of said function. That's all I need.
No offense, but you haven't explained yourself very well if neither of those is relevant to your problem. Perhaps you need both of them?
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
post #7 of 35
Thread Starter 
..Wow. Three times now, you have failed to completely go around my question. It's not a matter of my explanations; you're not reading what I'm trying to do!

Let me pull up the code for the part that needs the preset variable name, and literally insert what I mean:

$<This, the name, is the part that needs presetting as an argument, NOT the variable itself!!> = mysql_query("SELECT field_11 FROM dt_fields WHERE member_id='" . $userno . "'")

I can't just do this:
Code:

function(foo, $bar){

//insert lots of code here

$bar = mysql_query("SELECT field_11 FROM dt_fields WHERE member_id='" . $userno . "'")
or die(mysql_error());  

//insert more code here

Because when you define $bar inside the function, it is a preset variable with a name that isn't settable through an argument in the function call.

The script has to get the name to use for that variable from the second argument, so if the second argument is "apples", the variable should be named $apples. That is what I need, and this is the final time I'm explaining it, because if it's not clear by now, I don't know what you're thinking, really.
post #8 of 35
You might get more help by not yelling at the people trying to help. Error10's usually quite helpful with programming.

Also, I can't tell what it is that you're asking either.


In you exasperated example, would then $apples by a global variable? Or at least visible outside the function?
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
post #9 of 35
Quote:
Originally Posted by RevZ View Post
..Wow. Three times now, you have failed to completely go around my question. It's not a matter of my explanations; you're not reading what I'm trying to do!

Let me pull up the code for the part that needs the preset variable name, and literally insert what I mean:

$<This, the name, is the part that needs presetting as an argument, NOT the variable itself!!> = mysql_query("SELECT field_11 FROM dt_fields WHERE member_id='" . $userno . "'")

I can't just do this:
Code:

function(foo, $bar){

//insert lots of code here

$bar = mysql_query("SELECT field_11 FROM dt_fields WHERE member_id='" . $userno . "'")
or die(mysql_error());  

//insert more code here

Because when you define $bar inside the function, it is a preset variable with a name that isn't settable through an argument in the function call.

The script has to get the name to use for that variable from the second argument, so if the second argument is "apples", the variable should be named $apples. That is what I need, and this is the final time I'm explaining it, because if it's not clear by now, I don't know what you're thinking, really.
Oh, OK, so it IS variable variables that you want, just like I thought at first. My first response to you IS what you are looking for.

In case you need it spelled out:

Code:

$$bar = mysql_query("SELECT field_11 FROM dt_fields WHERE member_id='" . $userno . "'") 

If $bar is 'apples' then this will set $apples to the result of your query.
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
post #10 of 35
Thread Starter 
I'm not yelling at anyone; I am just completely baffled by the fact that seemingly code-adept people can't figure out the essence of what seems to be an extremely simple question to me.

I have a variable that is set inside a function, and I want to be able to define the name of that variable through an argument supplied with the function. I do NOT want to define a variable itself as an argument, but merely the name that will be given to it when it gets created inside the function.

I need this because the same function will be run over and over again, but the outgoing variable must never be overwritten and be accessible under the name I supply the function with. Hence I want to have a syntax like function(foo, bar) to get the value for foo in the database and put it in variable $bar.

(note the VERY important difference between bar as the argument and the variable $bar that should be what gets created inside the script; the former is whatever I give as a name, and the resulting variable should be named what I enter as an argument.)

Code:

function(field_13, one); 

Would store the contents of field_13 into a variable named $one.

Code:

function(field_16, two); 

Would store the contents of field_16 into a variable named $two.

Do you get it now? I absolutely don't see how one could not understand this.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › PHP: Variable name as argument in function