pokonipo Posted November 19, 2008 Share Posted November 19, 2008 Is it possible to detect the name of a variable that sent a string to a function? In other words, is it possible to do this... function myFunc($bbb) { // if $bbb was received from a variable named $aaa, echo "XYZ" } $aaa = "hello"; myFunc($aaa); Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/ Share on other sites More sharing options...
redarrow Posted November 19, 2008 Share Posted November 19, 2008 are you testing us at functions lol..... weired but done...... <?php $aaa="hello"; $bbb=$aaa; function myFunc($bbb) { echo $bbb; } $bbb=$aaa; echo myFunc($aaa); ?> Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693229 Share on other sites More sharing options...
genericnumber1 Posted November 19, 2008 Share Posted November 19, 2008 No, there isn't. And why could you ever want this? Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693230 Share on other sites More sharing options...
redarrow Posted November 19, 2008 Share Posted November 19, 2008 What it for it a floored idear m8............. what your idear? what you want to do? why are you getting me to write funny funky functions....... Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693233 Share on other sites More sharing options...
pokonipo Posted November 19, 2008 Author Share Posted November 19, 2008 A couple posters seem to be wondering why I would want to do this. So let me explain. I want to have a conditional statement in my function (as shown in the air code in my original post) so that I can easily change the behavior of the function simply by making a slight change (for example, from $aaa1 to $aaa2) in the code that sends the string to the function. I realize there are other ways to accomplish that, but I think this would be my easiest way, especially considering the fact that I have to change a ton of repetitive code on a huge complicated page. Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693234 Share on other sites More sharing options...
The Little Guy Posted November 19, 2008 Share Posted November 19, 2008 I believe this is what you want: function myFunk($bbb){ if(empty($bbb)){echo 'Nothing here';}else{'$bbb = '.$bbb;} } $aaa = "hello"; myFunk($aaa); Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693236 Share on other sites More sharing options...
genericnumber1 Posted November 19, 2008 Share Posted November 19, 2008 No there's no way to do this, and you should definitely reconsider your program design if that is what you require. Variable names should not have any influence on the program's logic. I would definitely advise you to remove the repetitive code if you're having trouble maintaining it. (and no, little guy, that's not what he wanted, he wanted to be able to see the var NAME "aaa" (not the value) in your code from inside the function, which is totally out of scope and on a different part of the stack) Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693238 Share on other sites More sharing options...
redarrow Posted November 19, 2008 Share Posted November 19, 2008 ive gon throw all your examples and it inpossable sorry give up... Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693240 Share on other sites More sharing options...
The Little Guy Posted November 19, 2008 Share Posted November 19, 2008 If you read his comment in the function, that is how it sounded to me... Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693241 Share on other sites More sharing options...
genericnumber1 Posted November 19, 2008 Share Posted November 19, 2008 Not to me, this is how it sounded to me... <?php function test($bbb) { if($bbb was set from $aaa) { // do something } elseif($bbb was set from $ccc) { // do something else } } $aaa = 'variable'; $ccc = 'another variable'; test($aaa); ?> ... which is impossible Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693244 Share on other sites More sharing options...
pokonipo Posted November 19, 2008 Author Share Posted November 19, 2008 So it is absolutely impossible for a function to know the name of a variable that sent a string to it? I kind of find that hard to beleive. Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693246 Share on other sites More sharing options...
redarrow Posted November 19, 2008 Share Posted November 19, 2008 what about this then be onest you got me ...... this shows that there a varable with redarrow in it so it is set and then set another varable to $aaa then show it......... <?php $bbb="redarrow"; function myfunction ($bbb){ switch ($bbb) { case isset($bbb): $aaa="XYZ"; echo $aaa; break; } } echo myfunction($bbb); ?> Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693249 Share on other sites More sharing options...
genericnumber1 Posted November 19, 2008 Share Posted November 19, 2008 @redarrow: I think you need some sleep @pokonipo: It's the way things work in all programming languages I've ever learned of (yes, what you describe is impossible in every single language I've ever encountered). Variables exist for human readability, the machine has no use for them, so when a programming language's code is compiled, human readable things such as constant names, variable names, etc are stripped in favor of byte code the machine can actually run. In addition to this, the variables exist on completely different planes when code is run. When a function is called it's pushed on top of something called the stack, inside this function's section of the stack is contained all of the variables declared in this particular function - it in the beginning has access to only the variables inside its scope plus all of the functions, classes, and global variables declared thus far. Access to other variables can be added to the current function's scope with use of the global keyword (you shouldn't do it btw, but that's a totally different discussion), but you must know the variable's name before hand. When the function returns, it's popped off of the stack and all of the variables inside of it are lost as well. It simply can not be done. What you describe would be a terrible implementation any way. Please bring your specific problem here and we can suggest to you an alternate solution, but I'm sorry to say yours is impossible. Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693252 Share on other sites More sharing options...
redarrow Posted November 19, 2008 Share Posted November 19, 2008 you can use var_dump, to see what inside the string but it inpossable to see what the name off the varable was..((forget it i give up..... <?php $bbb="redarrow"; function myfunction ($bbb){ switch ($bbb) { case isset($bbb): var_dump($bbb); $aaa="XYZ"; // echo $aaa; break; } } echo myfunction($bbb); ?> Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693256 Share on other sites More sharing options...
Zane Posted November 19, 2008 Share Posted November 19, 2008 genericnumber is absolutely right. what your asking is straight up impossible, although the concept that you seem to be looking for (or trying to skip) is called using flags. Maybe I'm wrong but the only reason I can come up with for wanting to know the source variable's name is to run an extra funciton or whatever.....so why not just use a flag? function myFunc($bbb, $flag==false) { // if $bbb was received from a variable named $aaa, echo "XYZ" if($flag != false) //etc etc } $aaa = "hello"; myFunc($aaa, "aaa"); Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693283 Share on other sites More sharing options...
Mark Baker Posted November 19, 2008 Share Posted November 19, 2008 So it is absolutely impossible for a function to know the name of a variable that sent a string to it? I kind of find that hard to beleive. Hard for you to believe or not, it's true. In fact, the function may not even have been passed a value from a variable. <?php function displayMe($text) { echo $text; } displayMe('Hello World'); ?> What is the name of the variable that I'm passing to the displayMe() function here? Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693290 Share on other sites More sharing options...
Mark Baker Posted November 19, 2008 Share Posted November 19, 2008 Try the following that generates a call stack trace to identify what was passed to a function: <?php function dumpStackTraceEntry($stacktraceCount,$stacktrace) { $output = $args = null; if ((isset($stacktrace['args'])) && (is_array($stacktrace['args']))) { foreach ($stacktrace['args'] as $a) { if (!is_null($args)) { $args .= ', '; } switch (gettype($a)) { case 'integer': case 'double': $args .= $a; break; case 'array': $args .= 'Array('.count($a).')'; break; case 'object': $args .= 'Object('.get_class($a).')'; break; case 'resource': $args .= 'Resource('.strstr($a, '#').')'; break; case 'boolean': $args .= $a ? 'True' : 'False'; break; case 'NULL': $args .= 'Null'; break; case 'string': $a = htmlspecialchars(substr($a, 0, 128)).((strlen($a) > 128) ? '...' : ''); $args .= "\"$a\""; break; default: $args .= 'Unknown'; } } } if ((isset($stacktrace['class'])) && (isset($stacktrace['type']))) { $output .= $stacktrace['class'].$stacktrace['type']; } $output .= "{$stacktrace['function']}(".$args.")<br />"; return $output; } // function dumpStackTraceEntry() function backTrace() { $output = '<h3>Stack Trace</h3>'; if (function_exists('debug_backtrace')) { $stacktraceDetails = debug_backtrace(); $stacktraceCount = count($stacktraceDetails); foreach ($stacktraceDetails as $stacktrace) { if (($stacktrace['function'] <> 'backTrace')) { $output .= dumpStackTraceEntry($stacktraceCount,$stacktrace); } $stacktraceCount--; } } echo ($output); echo '<hr />'; } function displayMe($value) { backTrace(); echo $value; } $xyz = 'Hello world'; displayMe($xyz); ?> You'll see that all the stack trace can retrieve is the value of the variable passed in to the function, not its name in the calling function Quote Link to comment https://forums.phpfreaks.com/topic/133291-detect-name-of-variable/#findComment-693302 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.