[PHP] Using Variable Variables from form values inside a function
Is it possible to use variable variables from form values inside a function. For example when submitting form values, this works fine: foreach($_POST as $key=$value) { if (empty($$key)) { print empty value $keybr; } } but, this doesn't: function myFunction() { foreach($_POST as $key=$value) { if (empty($$key)) { print empty value $keybr; } } } Anyone dealt with this? === Ron Dyck WebbTech www.webbtech.net [EMAIL PROTECTED] 905 734-1164 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Using Variable Variables from form values inside a function
- Original Message - From: Ron Dyck [EMAIL PROTECTED] but, this doesn't: function myFunction() { foreach($_POST as $key=$value) { if (empty($$key)) { print empty value $keybr; } } } Since php variable scope is local unless you define it global, that won't work that way. You might try (just guessing here): function myFunction() { foreach($_POST as $key=$value) { global $$key; if (empty($$key)) { print empty value $keybr; } } } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Using Variable Variables from form values inside a function
but, this doesn't: function myFunction() { foreach($_POST as $key=$value) { if (empty($$key)) { print empty value $keybr; } } } Since php variable scope is local unless you define it global, that won't work that way. You might try (just guessing here): function myFunction() { foreach($_POST as $key=$value) { global $$key; if (empty($$key)) { print empty value $keybr; } } } That did it. I assumed that since $_POST was global, the key/value pair would be. Guess not :-| Thanks! ron -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Using Variable Variables from form values inside a function
-Original Message- From: Ron Dyck [mailto:[EMAIL PROTECTED]] Sent: 18 February 2002 13:38 [... snip working example] but, this doesn't: function myFunction() { foreach($_POST as $key=$value) { if (empty($$key)) { print empty value $keybr; } } } What's wrong with: if (empty($_POST[$key])): ? This should work whether or not register_globals is switched on, and is therefore much to be preferred. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Using Variable Variables...
$_VARIABLE_ = chkContact.$i; $_VAR_ = $$_VARIABLE_; How do I make this work when $chkContact is a global variable? This returns an empty value because it does not see that $chkContact is a global variable... Thankz in advance for your help!! -- [ Swift eNetwork ] Matrix http://matrix.swifte.net/ -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Using Variable Variables...
on 7/26/01 6:46 PM, [EMAIL PROTECTED] at [EMAIL PROTECTED] wrote: $_VARIABLE_ = chkContact.$i; $_VAR_ = $$_VARIABLE_; How do I make this work when $chkContact is a global variable? This returns an empty value because it does not see that $chkContact is a global variable... maybe try global $_VARIABLE_, $$_VARIABLE_; -- mike cullerton -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]