Offer function, that returns variable's name.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sholerov
Forum Newbie
Posts: 1
Joined: Sun Jul 27, 2008 10:39 pm

Offer function, that returns variable's name.

Post by sholerov »

Hi!

For example, there is a script:
<?
echo some_function ($foobarbaz);
?>

This script returns string "foobarbaz".

What's the proper name of "some_function"?

Thanks!
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Offer function, that returns variable's name.

Post by jaoudestudios »

You can call the function whatever you want, as long as it is the same name when you define it!

i.e.

Code: Select all

 
echo function1($var);
 
 
function function1($v) {
 // do something
 return $v;
}
 
 
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Offer function, that returns variable's name.

Post by EverLearning »

Take a look at get_defined_vars function.
You'd call it from inside of your function to get the passed arguments.

Code: Select all

test(4, 5, 6);
 
function test($first, $second, $third) {
    var_dump(get_defined_vars());
}
And you get

Code: Select all

array
  'first' => int 4
  'second' => int 5
  'third' => int 6
 
Is that what you wanted?
Post Reply