topcoder1 Posted August 22, 2007 Share Posted August 22, 2007 many times I have a php page that accepts many parameters and validate them $userName=null; $password=null; $firstName=null; $lastName=null; if(isset($_POST['userName'])$userName=$_POST['userName']; ... is there a way to dynamically create variable and then assign a value to it? for example is it possible to dyanmically create a variable $userName? I am thinking about something like this: createVariable('userName'); $userName='hi'; thanks! Quote Link to comment https://forums.phpfreaks.com/topic/66196-create-dynamic-variable/ Share on other sites More sharing options...
akitchin Posted August 22, 2007 Share Posted August 22, 2007 in doing: $userName = 'hi'; you are dynamically creating it. do you mean is there a way to blanket-create all of your form fields as NULL before assigning them their POST values? or do you mean is there a way to create a variable variable name? Quote Link to comment https://forums.phpfreaks.com/topic/66196-create-dynamic-variable/#findComment-331108 Share on other sites More sharing options...
topcoder1 Posted August 23, 2007 Author Share Posted August 23, 2007 I mean is there a way to do something like this: $array=array("param1","param2"...); foreach($el in $array){ if(isset($_POST[$el]) createVariableAndAssignValueTo($el, $_POST[$el]); else createVariableAndAssignValueto($el,null); } //now I have all my params! echo($param1); echo($param2); does that make sense? thanks in doing: $userName = 'hi'; you are dynamically creating it. do you mean is there a way to blanket-create all of your form fields as NULL before assigning them their POST values? or do you mean is there a way to create a variable variable name? Quote Link to comment https://forums.phpfreaks.com/topic/66196-create-dynamic-variable/#findComment-331554 Share on other sites More sharing options...
Jessica Posted August 23, 2007 Share Posted August 23, 2007 <? $array=array("param1","param2"); foreach($array AS $el){ if(isset($_POST[$el]) $el = $_POST[$el]; }else{ $el = null; } } ?> Why are you trying to create a function for something simple like assigning a variable? Quote Link to comment https://forums.phpfreaks.com/topic/66196-create-dynamic-variable/#findComment-331566 Share on other sites More sharing options...
XaeroDegreaz Posted August 23, 2007 Share Posted August 23, 2007 Hi, you dynamically create and name your variables like this: $value = "Blue"; ${$value} = "Color"; echo ($Blue) //Prints out Color Same with arrays: $i[0] = "One"; $i[1] = "Two"; ${"newVariable".$i[0]} = "Wow"; echo($newVariableOne) //Prints out Wow Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/66196-create-dynamic-variable/#findComment-331652 Share on other sites More sharing options...
keeB Posted August 23, 2007 Share Posted August 23, 2007 Hi, you dynamically create and name your variables like this: $value = "Blue"; ${$value} = "Color"; echo ($Blue) //Prints out Color Same with arrays: $i[0] = "One"; $i[1] = "Two"; ${"newVariable".$i[0]} = "Wow"; echo($newVariableOne) //Prints out Wow Hope that helps. That is so retardedly stupid and unreadable. I can't ever think of a case where something like this is useful outside of obfuscation Thanks for sharing it, though! Quote Link to comment https://forums.phpfreaks.com/topic/66196-create-dynamic-variable/#findComment-331654 Share on other sites More sharing options...
XaeroDegreaz Posted August 23, 2007 Share Posted August 23, 2007 And.... You came in here to type all of that, but you didn't even offer a counter solution. Who's retarded here? Quote Link to comment https://forums.phpfreaks.com/topic/66196-create-dynamic-variable/#findComment-331668 Share on other sites More sharing options...
topcoder1 Posted August 23, 2007 Author Share Posted August 23, 2007 Hi, you dynamically create and name your variables like this: $value = "Blue"; ${$value} = "Color"; echo ($Blue) //Prints out Color Same with arrays: $i[0] = "One"; $i[1] = "Two"; ${"newVariable".$i[0]} = "Wow"; echo($newVariableOne) //Prints out Wow Hope that helps. thanks a lot! that's what I want! Quote Link to comment https://forums.phpfreaks.com/topic/66196-create-dynamic-variable/#findComment-331710 Share on other sites More sharing options...
topcoder1 Posted August 23, 2007 Author Share Posted August 23, 2007 <? $array=array("param1","param2"); foreach($array AS $el){ if(isset($_POST[$el]) $el = $_POST[$el]; }else{ $el = null; } } ?> Why are you trying to create a function for something simple like assigning a variable? It's mainly for reducing the lines of code I have to write, for example if I have 100 params like this that I have to assign individiually, it's roughly 100 lines of code. Now I can just do the above and have the same effect. it's very useful I think. Quote Link to comment https://forums.phpfreaks.com/topic/66196-create-dynamic-variable/#findComment-331712 Share on other sites More sharing options...
keeB Posted August 24, 2007 Share Posted August 24, 2007 And.... You came in here to type all of that, but you didn't even offer a counter solution. Who's retarded here? I am not bashing you, more the language in this regard. I didn't call you retarded, I hope you realize Quote Link to comment https://forums.phpfreaks.com/topic/66196-create-dynamic-variable/#findComment-332678 Share on other sites More sharing options...
teng84 Posted August 24, 2007 Share Posted August 24, 2007 i have actually use that in some case this ${x} helps me i use this in choosing what class to be used something like if ($x=='red') { ${red} == ill put here the class or css thing } i do this in the forms where if you missed it then it turns into red Quote Link to comment https://forums.phpfreaks.com/topic/66196-create-dynamic-variable/#findComment-332684 Share on other sites More sharing options...
dbo Posted August 24, 2007 Share Posted August 24, 2007 Yeah that is a pretty obscure thing and certainly could be construed as unreadable. If you choose to do something like that I'd suggest putting it off in a function rather than making code look convoluted. I'd be interested in hearing more about the problem you're trying to solve as there may be an alternative method. Quote Link to comment https://forums.phpfreaks.com/topic/66196-create-dynamic-variable/#findComment-332726 Share on other sites More sharing options...
topcoder1 Posted August 24, 2007 Author Share Posted August 24, 2007 Yeah that is a pretty obscure thing and certainly could be construed as unreadable. If you choose to do something like that I'd suggest putting it off in a function rather than making code look convoluted. I'd be interested in hearing more about the problem you're trying to solve as there may be an alternative method. in a typical php file that accepts parameters I do something like this: $a=null; $b=null; $c=null; //100 more lines like above if(isset($_POST['a'])) $a=$_POST['a']; if(isset($_POST['b'])) $a=$_POST['b']; if(isset($_POST['c'])) $a=$_POST['c']; //100 more lines like above //now business logic if($a !=null & b!=null){ } else if($a!=null && c!=null){ } else if($a==null && b==null & c!=null){ } now I can do this and saves lots of repeative code. $array=array("a","b", "c",...); // 1 line instead of 200 lines... foreach($array AS $el){ if(isset($_POST[$el]) $el = $_POST[$el]; }else{ $el = null; } } //then my business logic as usual if($a !=null & b!=null){ } else if($a!=null && c!=null){ } else if($a==null && b==null & c!=null){ } I am no php proficient. so Maybe there's other easy solution someone can point out. thanks Quote Link to comment https://forums.phpfreaks.com/topic/66196-create-dynamic-variable/#findComment-332781 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.