RE: how to get var name and value from function?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 16 April 2007 16:18, Tijnema ! wrote:

> On 4/16/07, Ford, Mike <M.Ford@xxxxxxxxxxxxxx> wrote:
> > On 14 April 2007 13:16, Afan Pasalic wrote:
> > 
> > > Tijnema ! wrote:
> > > > On 4/14/07, Afan Pasalic <afan@xxxxxxxx> 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;
> >    }
> > }
> 
> What's the sense in above function? you're putting the variables from
> 1 array in another... you could use array_merge for this.
> But even then it's quite useless...

No, not "just another array" (although I agree about the function being pretty useless!) -- $GLOBALS is a superglobal array that contains a reference to every variable defined in the global scope, so that accessing $GLOBALS['var'] from anywhere is the same as accessing $var in the global scope.  It's a way of referencing global variables without having to use a "global $var" statement.

I was simply pointing out how you can to "get info from function back to script" "when you don't know the keys", which you'd just said you believed was impossible! ;) ;)

Having done which, I proceeded to point out that:

> > ... 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.
> 
> That's a better idea. :)

... Precisely ;)

> >  foreach (array('name', 'address', 'email', 'setting1', 'setting2')
> >    as $key): $GLOBALS[$key] = $array[$key];
> >  endforeach;
> 
> endforeach? never heard of that statement before, does it
> really exist in PHP?

Of course -- would I give you non-working code? (Well, on purpose, anyway! ;) See http://php.net/manual/en/control-structures.alternative-syntax.php

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@xxxxxxxxxxxxxx
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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux