Jump to content


Photo

question


  • Please log in to reply
11 replies to this topic

#1 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 24 June 2006 - 01:53 AM

i have function:

function func(&$var)
{
echo "the name of variable passed is: ";
// echo the name of variable passed to the function.
}

for example, when I have:

$myhome = "This is my home";

//call
func($myhome);

// would print out:
"the name of variable passed is: myhome"

Is this possible guys?
is to get the name of the variable passed to a function, maybe reference or copy, doesn't matter.



Life's too short for arguing.

#2 dptr1988

dptr1988
  • Members
  • PipPipPip
  • Advanced Member
  • 372 posts

Posted 24 June 2006 - 02:22 AM

I don't think it is possible because the arguments passed to a function can be variables or constants.
Need more help with your project? One of the thousands of programmers, web designers or artists at <a href="http://www.rentacode...d_6764522">Rent A Coder</a> would be happy to help.

Disclaimer: Free advice is usually worth what you paid for it. ( or at least when it's coming from me! )

#3 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 24 June 2006 - 03:21 AM

i think so too,

but look at how Session handled this issue.

session_register($somevar);

and you can recall this $somevar in the session array:
$_SESSION['somevar']


Life's too short for arguing.

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 June 2006 - 06:22 AM

you could echo the variable name in single quotes

echo '$variable'

this would echo out

$variable

and i guess do a preg_replace or something to chop off the $.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 June 2006 - 11:32 AM

[!--quoteo(post=387358:date=Jun 24 2006, 04:21 AM:name=hvle)--][div class=\'quotetop\']QUOTE(hvle @ Jun 24 2006, 04:21 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
i think so too,

but look at how Session handled this issue.

session_register($somevar);

and you can recall this $somevar in the session array:
$_SESSION['somevar']
[/quote]
No that is not how sessions handle it or anyother function in php. It doesnt create a session var somevar if your variable is called $somevar. It creates the session variable based on the value of $somevar. So if you had this:
// assign $somevar the value of 'hello'
$somevar = "hello";
session_resgister($somevar);
It'll create a session variable based on the value of $somevar which will be hello and so the variable created is this: $_SESSION['hello']

It is not chopping of the dollor sign or anythink like that, it replaces the variable with its value. Thats how variables work.

#6 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 24 June 2006 - 11:15 PM

I can't chop off the $ sign cuz I do not know what the variable name is. (the variable passed to a function).

in the case of session:

$somevar = 'hello';
session_register('somevar');

echo $_SESSION['somevar']; // will echo hello

in this case, session_register magicly know the name of variable $somevar and bind this $_SESSION['somevar'] to the value of $somevar.
Life's too short for arguing.

#7 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 25 June 2006 - 12:07 AM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]session_register magic know the name of variable $somevar[/quote]
No... you are wrong. Read your post again, even it doesn't explain things this way.

#8 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 25 June 2006 - 04:08 AM

this is copy straight from manual.
I do not prefer session_register(), but I like to be able to do something similar to it.

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
session_register() accepts a variable number of arguments, any of which can be either a string holding the name of a variable or an array consisting of variable names or other arrays.

// Use of session_register() is deprecated
$barney = "A big purple dinosaur.";
session_register("barney");
[/quote]

it's deprecated however.

The deal is how it take a string 'barney' literately, then actually search the script for a variable with that name.

Perhap all variables in main (script) stored in some sort of array. If so, I want to find this magical array.

Life's too short for arguing.

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 June 2006 - 04:26 AM

All variables are stored in the superglobal array $_GLOBALS

If you do a
<?php echo '<pre>' . print_r($_GLOBALS,true) . '</pre>'; ?>
after defining a number of variables, you can see how they are stored.

Ken

#10 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 25 June 2006 - 05:22 AM

[!--quoteo(post=387661:date=Jun 25 2006, 02:26 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 25 2006, 02:26 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
All variables are stored in the superglobal array $_GLOBALS

If you do a
<?php echo '<pre>' . print_r($_GLOBALS,true) . '</pre>'; ?>
after defining a number of variables, you can see how they are stored.

Ken
[/quote]


You're right Ken, anyway, it's $GLOBALS.
They should name it $_GLOBALS to be consistent with other global vars thought.
Thank you.
Life's too short for arguing.

#11 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 25 June 2006 - 06:22 AM

maybe if you explain why you are trying to do this, we can help you find a satisfactory solution.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#12 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 25 June 2006 - 10:45 AM

Yes of course.

I'm writing a class to parse template. This class called SimpleTemplate.
The class works great, and i'm trying to improve it.

you have a template file in html like this:

<html>
<body>
Hello {{name}}!
</body>
</html>

you have an array like this:
$bundle['name'] = "Jessica";


The SimpleTemplate use this template and array, i.e $bundle and parse the value Jessica with the {{name}}.

So, instead of define an array like this:
$name = "Jessica";
$bundle['name'] = $name;

I will improve it like this:

$st = new SimpleTemplate();
$st->register('name');

this register function will automaticly create an item 'name' in the member variable and assign the value of $name.

Well, that's the idea.
Otherwise, the class work great. I know there're ton of class out there with similar purpose, but I prefer my own class, cuz I know exactly how well it perform. Efficiency is always important to me.

Life's too short for arguing.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users