Page 1 of 1

Create a variable with the name of a function's parameter?

Posted: Fri May 23, 2008 10:29 am
by JAB Creations
Let's say I have a function with a parameter of 'happy'. I want to be able to create a variable named 'happy' then (or whatever the parameter is that is called with the function. How would I do that?

Code: Select all

function example($property,$value)
{
if (isset($_POST['formis']))
 {
 if (isset($_POST['something'])) {//$property_parameter_var_here = '123';}
 }
}
*edit*

For clarification if we call the function as so...

Code: Select all

example('audio','1');
...then I want the variable to be named 'audio' (the value of the parameter passed to the function).

Re: Create a variable with the name of a function's parameter?

Posted: Fri May 23, 2008 10:38 am
by RobertGonzalez

Re: Create a variable with the name of a function's parameter?

Posted: Fri May 23, 2008 11:06 am
by JAB Creations
This works! What's the double dollar sign referred to as?

Code: Select all

function example($property,$value)
{
 global $$property;
 if (isset($_POST['formis']))
 {
  if (isset($_POST[$property])) {$$property = '123';}
 }
//static test but shows $audio is set  at the end of line 6 just above <!-- s:-) --><img src=\"{SMILIES_PATH}/icon_smile.gif\" alt=\":-)\" title=\"Smile\" /><!-- s:-) -->
 echo $audio;
}
 
example('audio','1');

Re: Create a variable with the name of a function's parameter?

Posted: Fri May 23, 2008 11:47 am
by RobertGonzalez
Variable variables. And that is pretty neat.

Re: Create a variable with the name of a function's parameter?

Posted: Fri May 23, 2008 11:53 am
by RobertGonzalez
I was just thinking... that may not work the way you want it to. Are you just trying to make a variable with the name of the string you pass to the function? And you want that in the global scope?

Re: Create a variable with the name of a function's parameter?

Posted: Fri May 23, 2008 11:46 pm
by JAB Creations
Are you referring to global scope in regards to functionality or security?

Re: Create a variable with the name of a function's parameter?

Posted: Sun May 25, 2008 9:45 am
by RobertGonzalez
I am just wondering why you are doing what you are doing in this way.

Re: Create a variable with the name of a function's parameter?

Posted: Mon May 26, 2008 3:41 pm
by JAB Creations
Uh..... because the voice in my head told me to. :mrgreen:

Also (coming off a few days of battling several failed XP reinstalls) I recall I needed to access the variable outside of the function.

Re: Create a variable with the name of a function's parameter?

Posted: Mon May 26, 2008 3:46 pm
by onion2k
I'm going to send Rasmus Lerdorf a giant cake with the words "Please remove the global operator" in pink icing on the top. Hopefully, like me, he is easily bribed with cake.

Re: Create a variable with the name of a function's parameter?

Posted: Mon May 26, 2008 4:17 pm
by JAB Creations
Keeping in mind that I'm currently loosing my grip on consciousness at the moment...

http://www.phpit.net/article/using-globals-php/

So two questions come to mind...

1.) If I declare globals would it be better to reserve doing so in only an included file designated for no other purpose (as to easily keep track/an alphabetical list to ensure I don't add too much funk to my work)?

2.) If/how can I simply make the $$property var. var. available to the rest of the file outside of the function without making it global. I think someone showed me that a while ago but I really haven't spent much time with functions until very recently.

Re: Create a variable with the name of a function's parameter?

Posted: Mon May 26, 2008 4:51 pm
by Zoxive
Echoing inside functions to me was always a bad idea.

Why not just return the data instead?

Also what exactly are you trying to accomplish?
From the looks of it, it's some sort of check if the POST data is empty, then if it is, default it to something.

Re: Create a variable with the name of a function's parameter?

Posted: Tue May 27, 2008 2:06 am
by JAB Creations
My goal is to execute an array of preferences defined either by POST, GET, or COOKIE (in that order). Each preference has a name (in example audio, connection, initialfocus) which are listed in an existing array (to avoid dealing with unwanted data). I then need to return what preference the function handled so I can define it as a class for usage around the rest of my serverside template.

I posted everything here...
viewtopic.php?f=50&t=83137

I already have had some thoughts for areas of improvement but have been battling goofed XP slipstream installs so I haven't been able to give the code as much time as I have wanted of late.

Re: Create a variable with the name of a function's parameter?

Posted: Tue May 27, 2008 7:59 am
by JAB Creations
YES! :mrgreen:

I'm very happy I've gotten to this point because this allowed me to make better usage of the function without having to (ab)use globals! Functions in PHP are a bit different then JavaScript (and I had been doing lots of JavaScript especially in the past two months). Any way to me JavaScript functions were a way of doing something and that's it. However in PHP it's not just a way of doing something but getting a reference based on the function. So as one can echo variables one can also echo functions! I don't know why PHP tutorials severely lack the usage of echo because in my book it will tell you exactly what is going on even if you're not getting an error message! :mrgreen:

The only thing I'm stumped on at the moment is that when there is not $_GET['audio'] set that my script echos 'audio' which I do understand as being the last part of the function ( return $property;) however I'm not sure how to write the script in my mind to fall back to the default. Or it could be somehow rewriting the foreach loop to only call the example() function in certain conditions.

But this is where has led me for now. Maybe I can beat you guys to a solution...heh maybe. :mrgreen: (solution being when $_GET['audio'] is not set it will echo as '0' and not as 'audio').

*edit* (It appears you can't conditionally return inside of {} within a function otherwise I would have moved on to the next step, or maybe I'm missing something in that regards?)

Code: Select all

<?php
$audio = '0';
 
function example($property)
{
 if ($_SERVER['REQUEST_METHOD'] == 'GET')
  {
   if (isset($_GET[$property])) {$property = $_GET[$property];}
  }
 return $property;
}
 
 
$cookie_old = array(
'audio',
'connection',
'theme'
);
 
foreach ($cookie_old as $key => $value) {
 example($value);
 // Transition Preview IV cookies to Preview V
 if (isset($_COOKIE[$value])) {setcookie($value, '', time()-60); $found = 1;}
 
 // Create $variable
 $$value = example($value);
}
?>
<html>
<head>
</head>
 
<body>
 
<div>
$audio = <?php echo $audio;?>
</div>
 
<div>
<a href="cookie6.php">No Request</a>
<br />
<a href="cookie6.php?audio=0">audio = 0</a>
<br />
<a href="cookie6.php?audio=1">audio = 1</a>
<br />
<a href="cookie6.php?audio=2">audio = 2</a>
</div>
 
</body>
</html>

Re: Create a variable with the name of a function's parameter?

Posted: Tue May 27, 2008 8:04 am
by JAB Creations
Ok haha actually thanks to onion2k's post in the critique forum (sorry about multiple threads haha) I realized that return will stop the function (which would help in regards to load I suppose) and so well...check out the code difference! I moved the return from between lines 9 and 10 to the end of line 8.

Now the issue has changed from $audio = 'audio' to how do get $audio = '0' as by default if none of the conditions in the function are met?

Code: Select all

<?php
$audio = '0';
 
function example($property)
{
 if ($_SERVER['REQUEST_METHOD'] == 'GET')
  {
   if (isset($_GET[$property])) {$property = $_GET[$property]; return $property;}
  }
}
 
 
$cookie_old = array(
'audio',
'connection',
'theme'
);
 
foreach ($cookie_old as $key => $value) {
 example($value);
 // Transition Preview IV cookies to Preview V
 if (isset($_COOKIE[$value])) {setcookie($value, '', time()-60); $found = 1;}
 
 // Create $variable
 $$value = example($value);
}
?>
<html>
<head>
</head>
 
<body>
 
<div>
$audio = <?php echo $audio;?>
</div>
 
<div>
<a href="cookie6.php">No Request</a>
<br />
<a href="cookie6.php?audio=0">audio = 0</a>
<br />
<a href="cookie6.php?audio=1">audio = 1</a>
<br />
<a href="cookie6.php?audio=2">audio = 2</a>
</div>
 
</body>
</html>