Jump to content

create dynamic variable


topcoder1

Recommended Posts

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

<?
$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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D

 

Thanks for sharing it, though!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

<?
$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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.