On Fri, April 13, 2007 7:49 pm, Afan Pasalic wrote: > I have to assign value of an array to variable named after key of the > array several times in my project to , e.g. after I submit a form with > personal info I have > $_POST['name'] = 'john doe'; > $_POST['address'] = '123 main st.'; > $_POST['city'] = 'urbandale'; > $_POST['zip'] = '12345'; > $_POST['phone'] = '123-456-7980'; > etc. > > Then I assign value to the var name: > foreach ($_POST as $key => $value) > { > ${$key} = $value; > } > and then validate submitted. > > Though, to avoid writing all over again the same lines (even it's only > 3 > lines) I was thinking to create a function something like: > > function value2var($array, $print=0) > { > foreach ($_POST as $key => $value) > { > ${$key} = $value; > echo ($print ==1) ? $key.': '.$value.'<br>'; // to test > results and seeing array variables and values //make it available to the rest of the script global ${$key}; > } > } > > value2var($_POST, 1); > > but, I don't know how to get info from function back to script?!?!? > :-( > > any help appreciated. Allow me to save you some pain... What you are doing *SEEMS* like a Good Idea to most PHP not-quite-beginners. It's not a Good Idea, however, because you end up with too many times where you end up adding some other include file that ends up using $name as a variable, and suddenly some other seemingly random portion of your script stops working -- and you usually don't connect the two, as the code you added to use $name worked "fine" for some time until you went to use that one FORM that used this value2var() function that makes $name global... Only $name as a global is problematic because you've used it elsewhere. Don't do what you are doing, because it's going to hurt you a lot eventually. Leave the stuff in $_POST, and deal with it early on, and put it explicitly in variables that you want to use for that script. Trying to generalize this and write a function and having globals all over the place will only cause you grief long-term. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php