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

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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