help w/ function to output var names, getting the var 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
JFrankParnell
Forum Newbie
Posts: 1
Joined: Sun Jun 07, 2009 11:59 am

help w/ function to output var names, getting the var name

Post by JFrankParnell »

Hi,

Code: Select all

$mice = array('mickey','minni','mighty');
$pets = array('cow'=>'bessie','dog'=>'fido','mice' => $mice);
$config = 'some setting';
 
function showvars($message){
    $args = func_get_args();
    echo"<pre>"; print_r($args);echo"</pre>";
    echo "$message <BR>";
    foreach ($args as $k=>$v) {
        $name = var_name($k,get_defined_vars());
        echo '$'.$name.' = '.$v."<br>";
    }
}
    
function var_name (&$iVar, &$aDefinedVars){
    foreach ($aDefinedVars as $k=>$v){
        $aDefinedVars_0[$k] = $v;
    }
    $iVarSave = $iVar;
    $iVar     =!$iVar;
 
    $aDiffKeys = array_keys (array_diff_assoc ($aDefinedVars_0, $aDefinedVars));
    $iVar      = $iVarSave;
 
    return $aDiffKeys[0];
 
}
 
echo var_name($mice,get_defined_vars())."<br>";// output: mice   (as expected)
echo showvars('heres the vars:',$pets,$config);
the goal here is to have showvars() output something like:
here's the vars:
$pets = array
$config = some setting


thanks for your time,
J
Last edited by Benjamin on Sun Jun 07, 2009 11:27 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply