hvle Posted June 24, 2006 Share Posted June 24, 2006 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";//callfunc($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. Quote Link to comment https://forums.phpfreaks.com/topic/12775-question/ Share on other sites More sharing options...
dptr1988 Posted June 24, 2006 Share Posted June 24, 2006 I don't think it is possible because the arguments passed to a function can be variables or constants. Quote Link to comment https://forums.phpfreaks.com/topic/12775-question/#findComment-48972 Share on other sites More sharing options...
hvle Posted June 24, 2006 Author Share Posted June 24, 2006 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 Link to comment https://forums.phpfreaks.com/topic/12775-question/#findComment-48979 Share on other sites More sharing options...
.josh Posted June 24, 2006 Share Posted June 24, 2006 you could echo the variable name in single quotesecho '$variable'this would echo out$variableand i guess do a preg_replace or something to chop off the $. Quote Link to comment https://forums.phpfreaks.com/topic/12775-question/#findComment-48999 Share on other sites More sharing options...
wildteen88 Posted June 24, 2006 Share Posted June 24, 2006 [!--quoteo(post=387358:date=Jun 24 2006, 04:21 AM:name=hvle)--][div class=\'quotetop\']QUOTE(hvle @ Jun 24 2006, 04:21 AM) [snapback]387358[/snapback][/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 [b]somevar[/b] if your variable is called $somevar. It creates the session variable based on the [b]value[/b] of $somevar. So if you had this:[code]// assign $somevar the value of 'hello'$somevar = "hello";session_resgister($somevar);[/code]It'll create a session variable based on the [b]value[/b] of [i]$somevar[/i] which will be [b]hello[/b] and so the variable created is this: [i]$_SESSION['hello'][/i]It is not chopping of the dollor sign or anythink like that, it replaces the variable with its value. Thats how variables work. Quote Link to comment https://forums.phpfreaks.com/topic/12775-question/#findComment-49032 Share on other sites More sharing options...
hvle Posted June 24, 2006 Author Share Posted June 24, 2006 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 helloin this case, session_register magicly know the name of variable $somevar and bind this $_SESSION['somevar'] to the value of $somevar. Quote Link to comment https://forums.phpfreaks.com/topic/12775-question/#findComment-49200 Share on other sites More sharing options...
trq Posted June 25, 2006 Share Posted June 25, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/12775-question/#findComment-49217 Share on other sites More sharing options...
hvle Posted June 25, 2006 Author Share Posted June 25, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/12775-question/#findComment-49268 Share on other sites More sharing options...
kenrbnsn Posted June 25, 2006 Share Posted June 25, 2006 All variables are stored in the superglobal array $_GLOBALSIf you do a [code]<?php echo '<pre>' . print_r($_GLOBALS,true) . '</pre>'; ?>[/code] after defining a number of variables, you can see how they are stored.Ken Quote Link to comment https://forums.phpfreaks.com/topic/12775-question/#findComment-49272 Share on other sites More sharing options...
hvle Posted June 25, 2006 Author Share Posted June 25, 2006 [!--quoteo(post=387661:date=Jun 25 2006, 02:26 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 25 2006, 02:26 PM) [snapback]387661[/snapback][/div][div class=\'quotemain\'][!--quotec--]All variables are stored in the superglobal array $_GLOBALSIf you do a [code]<?php echo '<pre>' . print_r($_GLOBALS,true) . '</pre>'; ?>[/code] 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. Quote Link to comment https://forums.phpfreaks.com/topic/12775-question/#findComment-49286 Share on other sites More sharing options...
.josh Posted June 25, 2006 Share Posted June 25, 2006 maybe if you explain why you are trying to do this, we can help you find a satisfactory solution. Quote Link to comment https://forums.phpfreaks.com/topic/12775-question/#findComment-49297 Share on other sites More sharing options...
hvle Posted June 25, 2006 Author Share Posted June 25, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/12775-question/#findComment-49335 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.