Jump to content

how to get variable name in function


madrazel

Recommended Posts

listvars('$f','$f');

 

hahah  ;D ;D ;D

 

whay would you want to?

 

Pass in the pointer to $f if you want to change it:

<?php

function xx( &$t )
{
$t++;
}

$t = 0;
xx( $t );

echo $t;

?>

 

See how this echos '1', the function xx has modified the variable $t because we passed a pointer to the function (the & means 'pointer to')

 

monk.e.boy

 

Have a look at var_dump.

 

But I can't see what finding out the name of the variable would do for you. Say we have a function called 'get_name' that would do what you want:

 

$a = 'boo';
echo get_name( $a ) .' = '. $a;

 

How is this better than just doing:

 

$a = 'boo';
echo '$a = '. $a;

 

? Eh?

 

Madness. For amazing debugging try getting NuSphere, PHPEd. It's brilliant.

 

monk.e.boy

 

 

whatever, i done this:

 


function listvars($x) { $args = explode(',',$x); ob_start();
foreach ($args as $val) { global $$val;
echo '$'.$val.' = ';
if (is_null($$val)) { echo "NULL"; }
elseif ($$val === FALSE) { echo "FALSE"; }
elseif ($$val === TRUE) { echo "TRUE"; }
elseif ($$val === '') { echo "EMPTY"; }
elseif (is_array($$val)) { print_r($$val);}
else { echo $$val; };
echo "\n";  };
echo "<pre>\n".ob_get_clean()."</pre>\n";
};

$a="t";
$b='';
$c="h";

listvars("a,b,c");

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.