Jump to content

Recommended Posts

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.

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)

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

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);

?>

@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.

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);

?>

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");

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?

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.