Forum Moderators: coopster

Message Too Old, No Replies

Can a function see with what exact paramenter it was called?

I call foo('hallo'). Can foo() find out it was a 'string' not a $variable?

         

knnknn

7:42 am on Feb 6, 2006 (gmt 0)

10+ Year Member



I read the relevant stuff in the PHP documentation, but I cannot find the solution:

Imagine a function
foo($x);

You can call it as
foo('hallo');
or as
foo($text);
or as
foo($text1.$text2);

Can foo() find out how it has been called? Whether its argument was "'hallo'" or "$text" or "$text1.$text2"?

Or at least what the name of the variable was ("$text")?

There is nothing written about that in the PHP docs in the topics about
* magic constants (like "__FUNCTION__")
* func_get_args()
* Variable-length argument lists
* variable references
* debug_backtrace()

ANY help is greatly appreciated!

dreamcatcher

7:53 am on Feb 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can run a test inside your function.


function ($text)
{

switch ($text)
{
case 'hello':
$msg = 'Hello Was Entered';
break

case 'goodbye':
$msg = 'Goodbye was entered';
break;
}

return $msg;

}

or isn`t that what you meant?

dc

DrDoc

8:08 am on Feb 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What knnknn is after is:

if I call the function using: function("blah") to return

"blah"

if I call the function using: function($foo.$bar) to return
$foo.$bar

if I call the function using: function("blah $bleh", $var) to return
"blah $bleh", $var

That kind of stuff ...

I think func_get_args [php.net] is what you want. Especially look at the user contributed examples.

knnknn

8:26 am on Feb 6, 2006 (gmt 0)

10+ Year Member



No, as I wrote in my first post, func_get_args() doesn't help.
It delivers only an index-based array

function foo()
{
print_r(func_get_args());
}

$text = 'hallo';
foo($text);

delivers


Array
(
[0] => hallo
)

instead of what I want to have


Array
(
['$text'] => hallo
)

DrDoc

8:40 am on Feb 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aah, yes ...