Pass argument variable name to function?

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
phpBuddy
Forum Commoner
Posts: 37
Joined: Mon Nov 05, 2007 3:42 am

Pass argument variable name to function?

Post by phpBuddy »

Code: Select all

<?php
function echo_variable(){
    $args = func_get_args();
    foreach($args AS $key => $val)
        echo $key.' - '.$val.'<br>';
}

// test 1
$name = 'john';
$age  = 20;
echo_variable($name, $age);
echo '<br>';
//test 2
$user = 'scoobydoo';
$mail = 'scobbydoo@hotmail.com';
echo_variable($user, $mail);
echo '<br>';

?>
I wonder if there is some way a function can get the name of a variable
which is passed to the function as an argument.

The result of my test:

Code: Select all

// test1
0 - john
1 - 20
//test2
0 - scoobydoo
1 - scobbydoo@hotmail.com
What I want:

Code: Select all

// test1
name - john
age - 20
//test2
user - scoobydoo
mail - scobbydoo@hotmail.com
Thanks!
Post Reply