RE: [PHP] how to get var name and value from function?
| From: | Ford, Mike | Date: | Mon, 16 Apr 2007 10:11:47 +0000 |
| Subject: | RE: [PHP] how to get var name and value from function? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-252993@lists.php.net to get a copy of this message | ||
On 14 April 2007 13:16, Afan Pasalic wrote:
> Tijnema ! wrote:
> > On 4/14/07, Afan Pasalic <afan@afan.net> wrote:
> > > function value2var($array, $print=0)
> > > {
> > > foreach ($_POST as $key => $value)
> >
> > I think you should change above line to :
> >
> > foreach ($array as $key => $value)
> yup! it's print error. I meant $array.
> > > {
> > > ${$key} = $value;
> > > echo ($print ==1) ? $key.': '.$value.'<br>'; //
> > > to test
> > > results and seeing array variables and values
> > > }
> > > }
> > >
> > > value2var($_POST, 1);
> > >
> > > but, I don't know how to get info from function back to
> > > script?!?!? :-(
> >
> > Uhm, it's not even possible when you don't know the keys i believe.
> after 2 hours of testing and research I realized this too, but want
> to be sure. :-(
If you really *must* do this yourself (but others have pointed out the folly of it), this would do
it:
function value2var($array)
{
foreach ($array as $key => $value)
{
$GLOBALS['$key'] = $value;
}
}
... or, alternatively, rather than defining you own function, use extract() (http://php.net/extract)
with one of the overwrite safety options to avoid blobbing existing variables.
Personally, I'd never do this in any form -- if I do it at all, I extract specific indices of
the array with code like:
foreach (array('name', 'address', 'email', 'setting1',
'setting2') as $key):
$GLOBALS[$key] = $array[$key];
endforeach;
... making certain, of course, that those values get properly validated elsewhere.
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 812 4730 Fax: +44 113 812 3211
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm