Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

possible to get the fieldname past to a function... 2

Status
Not open for further replies.

hos2

Programmer
May 6, 2002
418
NL
is it possible to retrieve the fieldname that is passed on to a function instead of the value ??


if I have a function like
Code:
function filledin($naam){
if ($naam <> "") {return 0;} else {return 1;}
}

and I use it like
filledin($name1);
filledin($name2);
filledin($name3);

can I also retrieve the name of the variable (name inside the function besides the value ??

 
Why don't you modify your application a bit.
Pass in this:

filledin("name1");
filledin("name2");
filledin("name3");

And change your function to:

function filledin($naam){
if ($$naam <> "") {return 0;} else {return 1;}
}
 
because I also need the value of name1 and name2

ofcourse I can write
filledin($name1,"name1");

but I'm a bit lazy progammer so I was wondering if I could skip the terrible burden of writing the same thing for the second time ;)
 
nop this doesn't work

if I do

function controle($functie,$veld,$veldnaam,$waarschuwing){
global $errorname;
global $returncode;

if ($functie == 'filledin') { $returncode=filledin($veld);}
if ($functie == 'emailvalid') { $returncode=emailvalid($veld);}

$errorname='error'.$veldnaam;
global $$errorname;


then it works fine but when I do
$errorname='error'.$$veld;


than it doesn't work :(
 
and also logical because $$naam2 makes a variable of the value of naam2 and not of naam2 itself
 
I don't understand what you mean.
What do you want to achive?

if let's say:

$test="BIG TEST";
$abc="test";
echo $abc."<BR>"; //return test
echo $$abc."<BR>"; // return BIG TEST

You can't combine it like this:
echo $abc.$$abc;
 
I want filledin($name1,"name1");

shorten to
filledin($name1);

and in the function retrieve the value of $name1 but also the name of the variable in this case name1 in a variable called fieldname
 
You might be running into a scope problem. If your function doesn't have the variable declared as global, it can't access its contents. And, you can't declare it if you don't know ahead of time what it is. Try this:

function show($t)
{
extract($GLOBALS);
echo "Variable: $t\n";
echo "Contents: ".$$t."\n";
}

$myvar="Variable Contents";
$abc="myvar";

$another="Another var's contents";
$def="another";

show($abc);
show($def);
 
A thanks this works yes and does what I asked

but I think my code would not be easy to read and implement with this structure. the fields in question are coming from $POST and now I have to make up a new name to store a fieldname I already have.






 
It's the Global Problem that was not working, not the part where we use "text" as a variable.
 
you're right, I missed interpreted it at first ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top