Jump to content

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
https://forums.phpfreaks.com/topic/66196-create-dynamic-variable/
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?

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?

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

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.

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!

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!

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

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

 

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.

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

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.